Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Is there a way to delete a subset (continous) of child nodes?

Reply
Thread Tools

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

 
 
Daniel Rucareanu
Guest
Posts: n/a
 
      09-22-2006
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

 
Reply With Quote
 
 
 
 
Patient Guy
Guest
Posts: n/a
 
      09-22-2006
"Daniel Rucareanu" <(E-Mail Removed)> wrote 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spreadsheet gem -- way to work with a subset of rows? Jim .. Ruby 0 12-19-2009 05:06 PM
The Easiest way to check if one array is a subset of another Thriving K. Ruby 2 08-04-2009 05:23 AM
Best way to compare two XML files for equivalence (subset) Tim Smith XML 0 09-29-2004 05:17 AM
EJB: How to delete child entries from a parent-child CMR one-to-many relationship? davout Java 0 04-18-2004 07:02 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments