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


D

Daniel Rucareanu

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
 
Ad

Advertisements

P

Patient Guy

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);
};
}
-----------------------------

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top