469,282 Members | 1,708 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Is there a way to delete a subset (continous) of child nodes?

Hello,

Does anybody knows how can you delete, in just one step, not using a
loop, a subset of the child nodes of a given DOM parent node? The
subset will be continous, so for example, if the parent node has 100
nodes, I want to delete nodes 10 through 75, and not nodes 5, 10, 25
etc.

I have a reference to the first and the last node in the list that has
to be removed. Also it's position in the list, if that helps.

Thank you,
Daniel

Sep 22 '06 #1
1 2077
"Daniel Rucareanu" <dr*************@gmail.comwrote in
comp.lang.javascript:
Hello,

Does anybody knows how can you delete, in just one step, not using a
loop, a subset of the child nodes of a given DOM parent node? The
subset will be continous, so for example, if the parent node has 100
nodes, I want to delete nodes 10 through 75, and not nodes 5, 10, 25
etc.
I don't know of a method defined in any standard or "recommendation."

What I have been doing is trying to add methods and/or properties to the
prototype object Node (the object from which all DOM element nodes are
derived). For example,
NOTE: untested code off the type of my head below

---- defined in dom1.js -----

if (typeof(Node.removeChildren) == "undefined")
{
Node.prototype.removeChildren = function (start, end) {
if (this.hasChildNodes() == false)
return (false); // no child nodes anyway
// lots of argument checking done in these next several lines
if (typeof(start) == "undefined")
start = 0;
else if (typeof(start) != "number")
return (false);
else if (start < 1)
start = 0; // could have it return an error instead
else
start--; // start index is zero-based
if (typeof(end) == "undefined")
end = this.childNodes.length;
else if (typeof(end) != "number")
return (false);
else if (end this.childNodes.length)
end = this.childNodes.length; // or return error
if (arguments.length == 1)
end = start + 1;
// now get on with removing child nodes
for (var i = start; i < end; i++)
this.removeChild(this.childNodes[i]);
};
}
-----------------------------

Note there is a line about arguments.length. One might want it such that
if there is only one argument, only the one node at the index is removed.

So in your HTML document, include the 'dom1.js' external script file.

You can call .removeChildren() method in one of three ways:

myNode.removeChildren(); // removes all child nodes
myNode.removeChildren(10); // removes node 10 (indexed as 9)
// note it does not mean, start removing all nodes from node 10
myNode.removeChildren(10, 75); // should remove nodes 10-75,
// which would be indexed from 9-74

Ecept for the zero-argument form, it seems that removing nodes based on
the index they occur in the child nodes array has limited utility. A
better method would be to traverse (treewalk) nodes based in a node
identity/tag name/element type and select nodes on that basis.

I have a reference to the first and the last node in the list that has
to be removed. Also it's position in the list, if that helps.

Sep 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Martin Magnusson | last post: by
3 posts views Thread by Hrvoje Voda | last post: by
7 posts views Thread by amruta | last post: by
2 posts views Thread by daz_oldham | last post: by
1 post views Thread by tvks | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.