Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to delete a range of nodes..

    Hello,

    I am trying to delete a selection of nodes as follows:

    Code:
    newRange = document.selection.createRange();
    newRange.selectNodeContents(document.getElementById("id"));
    newRange.deleteContents();
    But I get a javascript error on line2:
    Object doesn't support this property or method.

    Could anyone please tell how do I delete all the children and sub children of a tag in an Html document?

    Thanks a lot!
    Last edited by liorean; 05-21-2004 at 01:14 PM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    newRange = document.createRange();

  • #3
    New Coder
    Join Date
    Feb 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have tried using document.createRange() as well. In that case also, I get the same javascript error:

    Object doesn't support this property or method.

    Thanks!

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    You're probably using Internet Explorer than. Their developers implemented a TextRange object, which they often call simply a Range. However, the W3C DOM working group also created a Range object in DOM2. IE's TextRange object is not compliant at all with the DOM2 Range specifications, even though you sometimes see similarly named methods (createRange() for instace). That is probably what is confusing you.

    Try using document.createRange() and the rest of your code in Mozilla. It will work fine.

  • #5
    New Coder
    Join Date
    Feb 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your response! I have figured out a way of removing nested elements in an html tag in IE.

    Regards,
    Mamta.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, if you just want to limit it to childrens of a certain element, it's easy. If you want it to be the elements contained in a selection it gets trickier. To remove from a single element and all it's children you simply do:
    Code:
    element.parentNode.removeChild(element);
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    New Coder
    Join Date
    May 2004
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mamtakansal,

    I was wondering . . .

    Would you be willing to share how you got it working with IE?


  • #8
    New Coder
    Join Date
    Feb 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure. I had variable number of rows with two fixed columns in the datatable. Here is what I did to remove the elements in the datatable:

    Code:
       while (tablebody.hasChildNodes()) {   // while tbody has datarow elements
                tablebody.firstChild.firstChild.nodeValue = null;
                tablebody.firstChild.lastChild.nodeValue = null;
                while (tablebody.firstChild.hasChildNodes()) {
                    tablebody.firstChild.removeChild(tablebody.firstChild.firstChild);
                }
                tablebody.removeChild(tablebody.firstChild);
            }
            datatable.removeChild(tablebody);
    Hope it helps!
    Last edited by liorean; 05-21-2004 at 01:14 PM. Reason: [code]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •