We help IT Professionals succeed at work.
Get Started

Recursive use of replaceWith behaving differently on IE9.0.8112.16421 than FireFox 7.0.1

CAS-IT asked
Last Modified: 2012-08-13
We've got a function that we point at a specific element, and it recurses through every child-element, with the intent of removing the tags which do not match our 'approved tag' list.  approved tags are passed in as an argument of type array of string.

In Firefox, it works perfectly.

In IE, it does not fail, but it drops out a lot of the content once it hits the first span.

Specific function is here...
function cleanHTML (theObject,allowedTags){
	for (i=$(theObject).find('*').length-1;i>=0;i--) {
		if ($.inArray($($(theObject).find('*')[i]).get()[0].tagName.toLowerCase(),allowedTags)==-1)
			{ $($(theObject).find('*')[i]).replaceWith($($(theObject).find('*')[i]).html());}

Open in new window

Downloadable HTML file you can run to test the function out here...

HTML Source of our test page is here...
<!DOCTYPE html>
	<title>cleanHTML Test v3.0 - Modified 11/3/2011</title>
	<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function cleanHTML (theObject,allowedTags){
	for (i=$(theObject).find('*').length-1;i>=0;i--) {
		if ($.inArray($($(theObject).find('*')[i]).get()[0].tagName.toLowerCase(),allowedTags)==-1)
			{ $($(theObject).find('*')[i]).replaceWith($($(theObject).find('*')[i]).html());}

<div ID="testcontainer">
	<div style="border:1px solid blue;" class="slJobDuties">
		<div style = "border:1px solid red;" class="ExternalClass936C2C07EC22467780317A758292E763">
			<div style="url:'http://www.udel.edu'">
			<p StYle='font-weight:bold;'>
				Text <a href=".">Outside</a> <i>Spans</i>
				<span class="bob">
				Test Inside one span, outside of others
				<span style="border:1px solid black;">
				<span style="border:1px solid orange;" class="bob">
				<span class="bob" style="border:1px solid orange;">
					College <i><a href=".">Accounting</a></i> <span><b>Style</b></span>
					<br />
					More stuff

console.log($("#testcontainer").html()); //Handy Debugging line - show me the HTML before I do stuff
console.log($("#testcontainer").html()); //Handy Debugging line - show me the HTML AFTER I do stuff


Open in new window

Watch Question
Owner (Aidellio)
Most Valuable Expert 2015
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE