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

Discussion in 'Javascript' started by Daniel Rucareanu, Sep 22, 2006.

  1. 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
     
    Daniel Rucareanu, Sep 22, 2006
    #1
    1. Advertising

  2. Daniel Rucareanu

    Patient Guy Guest

    "Daniel Rucareanu" <> 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);
    };
    }
    -----------------------------

    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.
     
    Patient Guy, Sep 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnny
    Replies:
    0
    Views:
    898
    Johnny
    Oct 2, 2003
  2. davout
    Replies:
    0
    Views:
    553
    davout
    Apr 18, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,132
    David Schwartz
    Dec 9, 2003
  4. Replies:
    1
    Views:
    578
    Soren Kuula
    Aug 24, 2006
  5. Replies:
    1
    Views:
    1,208
    Joe Kesselman
    Nov 1, 2006
Loading...

Share This Page