[thelist] DOM - removeNode() problems
Simon Willison
cs1spw at bath.ac.uk
Tue Aug 5 04:57:36 CDT 2003
Hi Jeff,
Tuesday, August 5, 2003, 4:10:00 AM, you wrote:
> what strikes me as funny is that both methods are practically identical.
> it's too bad there's not a removeChildNodes() method that would clear out
> all children of an object in one method call.
So write one :)
<script type="text/javascript">
Node.prototype.removeChildNodes = function() {
while (this.childNodes.length > 0) {
this.removeChild(this.childNodes[0]);
}
}
</script>
<div id="test">
Aah, <strong>sweet</strong> DOM
</div>
<input type="button" value="click me"
onclick="document.getElementById('test').removeChildNodes()">
This works fine in Mozilla, which allows you to add methods to the DOM
Node object. IE doesn't expose this, to a more cross browser
compatible version would look like this:
document.removeChildNodes = function(node) {
while (node.childNodes.length > 0) {
node.removeChild(node.childNodes[0]);
}
}
Tested with:
<input type="button" value="click me"
onclick="document.removeChildNodes(document.getElementById('test'))">
Adding it as a method on the document object isn't really any
different from defining it as a function, but at least it keeps it out
of the global namespace. It's a shame IE doesn't expose the DOM
objects completely or we could have all kinds of fun with this sort of
thing.
Cheers,
Simon
--
http://simon.incutio.com/
More information about the thelist
mailing list