Results 1 to 5 of 5

Thread: How can I "port" elements?

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I "port" elements?

    Hello everyone,

    Can anyone tell me how do I "port" elements from one node to other? What I mean is, if there is an element tree like this:

    Code:
    div_one >> div_1 -- div_2 -- div_3 
    div_two >> div_4 -- div_5
    (>> means child and -- means sibling)

    Now how do I change it to

    Code:
    div_one >> div_1 -- div_2 -- div_3 -- div_4 -- div_5
    div_two
    dynamically with javascript?

    I hope I was able to clear my point.

    Thank you for your time in this post.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    Code:
    div_one >> div_1 -- div_2 -- div_3 
    div_two >> div_4 -- div_5
    (>> means child and -- means sibling)

    Now how do I change it to

    Code:
    div_one >> div_1 -- div_2 -- div_3 -- div_4 -- div_5
    div_two
    Use the appendChild and insertBefore methods. If the element already exists in the document tree, it's removed from its current parent and reattached in the new location.

    In this exact case, you could write something like:

    Code:
    while (div_two.firstChild)
        div_one.appendChild(div_two.firstChild);
    to remove all child nodes (not just elements), where div_one and div_two are references to elements.

    Mike

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot mwinter. I think I'll use the appendChild method.

    If the element already exists in the document tree, it's removed from its current parent and reattached in the new location.
    Do you mean that if the element exists appendChild will automatically remove the element and append it to it's new parent?

    while (div_two.firstChild)
    div_one.appendChild(div_two.firstChild);
    That's not "exactly" what I want. What I want is something like a sortable list in which a user can manipulate the elements.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    If the element already exists in the document tree, it's removed from its current parent and reattached in the new location.
    Do you mean that if the element exists appendChild will automatically remove the element and append it to it's new parent?
    Yes.

    That's not "exactly" what I want.
    It's what you described, though you didn't discuss other nodes that might exist. I can't help it if the two aren't the same.

    Mike

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    It's what you described, though you didn't discuss other nodes that might exist. I can't help it if the two aren't the same.
    I thought you already understood what I wanted. Sorry for the mistake.

Bookmarks

Posting Permissions

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