472,351 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

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 2201
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Magnusson | last post by:
I'm getting segmentation faults when trying to fix a memory leak in my program. The problem is related to lists of pointers which get passed around...
13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get...
3
by: Hrvoje Voda | last post by:
I manage to fill treeview control with data and I would like to search through that data. In child I have names of films, and when I click on one...
7
by: amruta | last post by:
the code below dows not let me get the parent child view... all the nodes are show in one line only... also i need them to be collasped ... ...
2
by: daz_oldham | last post by:
Hi everyone An easy one I think, I am just not too sure of the best way of going about this. I have the following XmlNode as an example: ...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in...
1
by: tvks | last post by:
Dear all, I have an xml file called abc.xml. It is of the following format. <?xml version="1.0" encoding="ISO-8859-1"?> <contacts> ...
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form...
31
by: matthewslyman | last post by:
I have an unusual design and some very unusual issues with my code... I have forced Access to cooperate on everything except one issue - record...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.