473,782 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 removeAllChildN odes(node) {
if (node && node.hasChildNo des && node.removeChil d) {
while (node.hasChildN odes()) {
node.removeChil d(node.firstChi ld);
}
}
} // removeAllChildN odes()

or

function removeAllChildN odes(node) {
if (node && node.parentNode && node.parentNode .replaceChild
&& node.cloneNode) {
node.parentNode .replaceChild(n ode.cloneNode(f alse),
node);
}
} // removeAllChildN odes()

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*****@agrico reunited.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 12595

"Grant Wagner" <gw*****@agrico reunited.com> wrote in message
news:40******** *******@agricor eunited.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 removeAllChildN odes(node) {
if (node && node.hasChildNo des && node.removeChil d) {
while (node.hasChildN odes()) {
node.removeChil d(node.firstChi ld);
}
}
} // removeAllChildN odes()

or

function removeAllChildN odes(node) {
if (node && node.parentNode && node.parentNode .replaceChild
&& node.cloneNode) {
node.parentNode .replaceChild(n ode.cloneNode(f alse),
node);
}
} // removeAllChildN odes()

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*****@agrico reunited.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*****@agrico reunited.com> wrote in message
news:40******** *******@agricor eunited.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.******@blueyo nder.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 removeAllChildN odes(node) {
if (node && node.hasChildNo des && node.removeChil d) {
while (node.hasChildN odes()) {
node.removeChil d(node.firstChi ld);
}
}
} // removeAllChildN odes()

or

function removeAllChildN odes(node) {
if (node && node.parentNode && node.parentNode .replaceChild
&& node.cloneNode) {
node.parentNode .replaceChild(n ode.cloneNode(f alse),
node);
}
} // removeAllChildN odes()

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 removeAllChildN odes(node) {
if (node && node.parentNode && node.parentNode .replaceChild
&& node.cloneNode) {
node.parentNode .replaceChild(n ode.cloneNode(f alse),
node);
}
} // removeAllChildN odes()

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*****@agrico reunited.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
4167
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 get *9*. In the example table, there are 4 rows with 4 columns each in the tbody. I'd expect 4 child nodes for the table body with 4 children each. I get those 4 plus 5 alerts in Mozilla/Netscape. I get only those 4 in IE and Opera. Note that...
3
16040
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
10701
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 attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
12
20532
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()); treeView1.Nodes.Add(tnode); //till here code works fine //now I wanna add child nodes for last inserted node foreach( DataRow GrupaRow in TabelaGrupe.Rows)
1
718
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 DialogBox. I'd like the user to be able to remove one or more children nodes by selecting them. But if I use the following syntax: Dim Mynode As TreeNode Mynode = TreeView1.SelectedNode.Nodes(0) Mynode.Remove()
7
3770
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
2980
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 xmlns present is because the Xml I am passing to CRM is a node from a bigger file that does require a xmlns and using the DOM ..OuterXml seems to set the xmlns for you automatically - which I don't want. Any help would be great.
1
1254
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 that i want but i need to change some xml node value before i insert. what is the best way to do this?
5
7824
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 the actual xml nodes about by interacting with the non-xml TreeView nodes. my basic algorithm is as follows: user has selected a treeview node if user presses Ctrl+Up Arrow, then
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.