Link to home
Start Free TrialLog in
Avatar of mychel_normandeau
mychel_normandeauFlag for Canada

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.getElementById("item2"));

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 ??
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mychel_normandeau

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...
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...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why a "B" ?