473,889 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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

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,

Sep 22 '06 #1
1 2337
"Daniel Rucareanu" <dr************ *@gmail.comwrot e in
comp.lang.javas cript:

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
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.re moveChildren) == "undefined" )
Node.prototype. removeChildren = function (start, end) {
if (this.hasChildN odes() == 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
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.leng th == 1)
end = start + 1;
// now get on with removing child nodes
for (var i = start; i < end; i++)
this.removeChil d(this.childNod es[i]);

Note there is a line about arguments.lengt h. 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.removeCh ildren(); // removes all child nodes
myNode.removeCh ildren(10); // removes node 10 (indexed as 9)
// note it does not mean, start removing all nodes from node 10
myNode.removeCh ildren(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

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 between objects. Here is a description of how the code works: I have a stack consisting of nodes and arguments, much like a function execution stack. In this example there are only three nodes on the stack. For each run through the loop, a copy of the current sensation is written to the...
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...
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 of them I would like to show it's information in grid. How? Hrcko
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 ..
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: <people group="33" status="334kdnsfins435" value"x"> <person id="1" name="tom" /> <person id="2" name="sue" />
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 which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
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> <entry uri="sip:vinayg@company.org"> <name>Vinay</name> <sip> sip:vinayg@company.org </sip> <Number> 9885821530 </Number>
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 with a (continuous) subform, and on the subform I have a command button, called cmdDelete, that launches a dialog form, called frmDeleteCurrentRecord, where the user has to enter their name and a reason. I have managed, through a ton of trial and tribulation, found a way to delete the current...
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 deletion. My form design involves a recursively nested form. In other words, the form, m_settings_menueditor_recursive has a single subform; m_settings_menueditor_recursive (both are viewed as datasheets - so the form is its own subdatasheet.) The Form_Open event modifies the form's recordset so...
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...
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,...
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...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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,...
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();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.