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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to use cloneNode

    i have a div, "abomb" that i want to clone. there is nothing on the page besides this div, and i don't want any depth in the hierarchy. i want the clone to just be a sibling of 'abomb'. how do i do it?

    'If you don't stand for something, you'll fall for anything.'

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i figured it out.
    it goes:
    var el=document.getElementById("abomb").cloneNode();
    document.body.appendChild(el);
    'If you don't stand for something, you'll fall for anything.'

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    If abomb has any children (stuff inside the element, such as text or other elements), you want to use cloneNode(true).
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I may be wrong here, but I believe

    document.body.appendChild(el);

    will not create the sibling relationship you wanted. I think

    document.getElementById("abomb").parentNode.appendChild(el);

    would do it.

    .... so I guess

    document.getElementById("abomb").previousSibling.appendChild(el);

    would make them cousins
    Last edited by brothercake; 10-05-2002 at 06:36 AM.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    document.getElementById("abomb").parentNode.appendChild(el);
    yea that looks right. but if 'abomb' is the only thing on the page, then 'body' is after all its parentNode, so i guess document.body.appendChild(el) and the above one are pretty much the same thing ... or aren't they?

    by the way, i couldn't get any cousins because 'abomb' doesn't have any previous siblings... if it did, though, i don't think appending a child to the previous sibling would keep the hierarchy flat, it would make 'abomb' el's uncle (or aunt ... )
    'If you don't stand for something, you'll fall for anything.'

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't see the bit about only have that node on the page - in that case, yes, it amounts to the same thing.

    And you're right - it would be cousin/uncle.

    document.getElementById("me").previousFlatmate.nextLover.parentNode.stepSister ...


  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    lol
    'If you don't stand for something, you'll fall for anything.'


  •  

    Posting Permissions

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