473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Best way of removing child nodes

There doesn't seem to be any mechanism to "clear" a node of all
it's children (not that its necessary very often, but I have come
across situations where I'd like to clear a node of all it's
children before appending other nodes). I've come up with two
possibilities:

function removeAllChildNodes(node) {
if (node && node.hasChildNodes && node.removeChild) {
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
}
} // removeAllChildNodes()

or

function removeAllChildNodes(node) {
if (node && node.parentNode && node.parentNode.replaceChild
&& node.cloneNode) {
node.parentNode.replaceChild(node.cloneNode(false) ,
node);
}
} // removeAllChildNodes()

Which do you like better and why? Is there an easier method or
way of clearing the children of a node that I'm missing?

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #1
4 12568

"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:40***************@agricoreunited.com...
There doesn't seem to be any mechanism to "clear" a node of all
it's children (not that its necessary very often, but I have come
across situations where I'd like to clear a node of all it's
children before appending other nodes). I've come up with two
possibilities:
I seem to recall that removeNode(true), will remove all child nodes.

Jeff

function removeAllChildNodes(node) {
if (node && node.hasChildNodes && node.removeChild) {
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
}
} // removeAllChildNodes()

or

function removeAllChildNodes(node) {
if (node && node.parentNode && node.parentNode.replaceChild
&& node.cloneNode) {
node.parentNode.replaceChild(node.cloneNode(false) ,
node);
}
} // removeAllChildNodes()

Which do you like better and why? Is there an easier method or
way of clearing the children of a node that I'm missing?

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/...3/reference/fr
ames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...l_reference_en
try.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 23 '05 #2
On Fri, 23 Apr 2004 22:53:32 GMT, Jeff Thies <no****@nospam.net> wrote:
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:40***************@agricoreunited.com...
There doesn't seem to be any mechanism to "clear" a node of all
it's children (not that its necessary very often, but I have come
across situations where I'd like to clear a node of all it's
children before appending other nodes). I've come up with two
possibilities:


I seem to recall that removeNode(true), will remove all child nodes.


[snip]

There is no such method in DOM Core. The Node interface does include
removeChild(), which accepts a Node reference, but Grant already included
that in his post.

Mike
Please trim your quotes.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3
Ivo
"Grant Wagner" wrote
There doesn't seem to be any mechanism to "clear" a node of all
it's children (not that its necessary very often, but I have come
across situations where I'd like to clear a node of all it's
children before appending other nodes). I've come up with two
possibilities:

function removeAllChildNodes(node) {
if (node && node.hasChildNodes && node.removeChild) {
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
}
} // removeAllChildNodes()

or

function removeAllChildNodes(node) {
if (node && node.parentNode && node.parentNode.replaceChild
&& node.cloneNode) {
node.parentNode.replaceChild(node.cloneNode(false) ,
node);
}
} // removeAllChildNodes()

Which do you like better and why?
The first is shorter, easier to read, and the use of a while loop seems
appropriate.
Is there an easier method or
way of clearing the children of a node that I'm missing?


Perhaps create an empty copy with cloneNode(false), remove the node itself
from its parent, and add the clone in its place. HTH
Ivo
Jul 23 '05 #4
Ivo wrote:
function removeAllChildNodes(node) {
if (node && node.parentNode && node.parentNode.replaceChild
&& node.cloneNode) {
node.parentNode.replaceChild(node.cloneNode(false) ,
node);
}
} // removeAllChildNodes()

Which do you like better and why?


Perhaps create an empty copy with cloneNode(false), remove the node itself
from its parent, and add the clone in its place. HTH
Ivo


That's what the code shown does, except it just does it all at once.

Thanks for the feedback, I've gone with the while() loop version removing
firstChild on each iteration.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 the expected 4 alerts. In Mozilla/Netscape, I...
3
by: e-mid | last post by:
Here is an xml structure. i want to remove <a> nodes that do not have any child. How can i do that in csharp? <root> <a> <b/> </a> <a/> <a/> <a> <c/>
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
12
by: Dino L. | last post by:
I am putting data from DataTable to treeView foreach( DataRow aRow in aTable.Rows) { TreeNode tnode = new TreeNode(aRow.ToString() + aRow.ToString() + " " + aRow.ToString());...
1
by: Progalex | last post by:
Hi, I have a treeview, called Treeview1 with one father node and some children nodes. Number of children nodes can change since every child node is a file name that the user add through an Open...
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 ... Thanks ..
7
by: Simon Hart | last post by:
Hi, I have a requirement to remove the xmlns from the DOM in order to pass over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a xmlns present!?! The reason I have a...
1
by: dotnetnoob | last post by:
i need to copy several xml files and use it as xml element and insert them into a xml file. i use DOM support in .Net and from the pervious help i was able to copy and insert them into the xml file...
5
WebDunce
by: WebDunce | last post by:
Hi guys, I am trying to develop a simple xml editor. I have an object that has a TreeView. I use the TreeView to display the Xml node info. That's all fine, but i want the user to be able to move...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.