mychel_normandeau
asked on
AJAX + DOM (JavaScript) ClearText function
I have a JavaScript function that removes all the child nodes of the specified element (el).
I use it this way:
ClearText(document.getElem entById("i tem2"));
HTML looks like this:
<table>
<th>
<td>Header</td>
</th>
<span id="item1">
<tr>
<td>1</td>
</tr>
</span>
<span id="item2">
<tr>
<td>2</td>
</tr>
</span>
<span id="item3">
<tr>
<td>3</td>
</tr>
</span>
</table>
Function looks like this:
function ClearText(el)
{
if (el != null)
{
if (el.childNodes)
{
for (var i = 0; i < el.childNodes.length; i++)
{
var childNode = el.childNodes[i];
el.removeChild(childNode);
}
}
}
}
Everything works well in IE7 but don't work in Firefox... what's wrong ??
I use it this way:
ClearText(document.getElem
HTML looks like this:
<table>
<th>
<td>Header</td>
</th>
<span id="item1">
<tr>
<td>1</td>
</tr>
</span>
<span id="item2">
<tr>
<td>2</td>
</tr>
</span>
<span id="item3">
<tr>
<td>3</td>
</tr>
</span>
</table>
Function looks like this:
function ClearText(el)
{
if (el != null)
{
if (el.childNodes)
{
for (var i = 0; i < el.childNodes.length; i++)
{
var childNode = el.childNodes[i];
el.removeChild(childNode);
}
}
}
}
Everything works well in IE7 but don't work in Firefox... what's wrong ??
Perhaps because FF is standards compliant and does not view the row as a valid child of a span?
This works (notice the removed whitespace
<table border="1">
<th>
<td>Header</td>
</th>
<tr id="item1">
<td>1</td>
</tr>
<tr id="item2"><td>2</td></tr>
<tr id="item3">
<td>3</td>
</tr>
</table>
<script>
function ClearText(el)
{
if (el != null)
{
if (el.childNodes)
{
for (var i = 0; i < el.childNodes.length; i++)
{
var childNode = el.childNodes[i];
el.removeChild(childNode);
}
}
}
}
ClearText(document.getElementById("item2"));
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mplungjan: what have you done exactly ? I removed the <span></span> and used the id on the <tr> instead, but still don't work...
ASKER
Ok, it "works with cleanWhitespace but it's not doing what i've expected... In my table I have many cols per row, and now it only delete the first one.
I want to clear the whole row...
I want to clear the whole row...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why a "B" ?