SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replaceChild and keep all childs

    Hi,

    I'm trying to change the order of a few P nodes, so I decided to use replaceChild function.
    Here's the relevant HTML:
    HTML Code:
    <div>
    	<p>item 1</p>
    	<p>item 2</p>
    	<p>item 3</p>
    	<p>item 4</p>
    	<p>item 5</p>
    	<p>item 6</p>
    </div>
    I want the result to look like this:
    HTML Code:
    <div>
    	<p>item 3</p>
    	<p>item 2</p>
    	<p>item 6</p>
    	<p>item 4</p>
    	<p>item 5</p>
    	<p>item 1</p>
    </div>
    But replaceChild deletes 1 node each time. The only thing I came up with is to save a copy of the node that's going to be erased, and the create it again (using createElement). Is there a better way to accomplish this?
    /(bb|[^b]{2})/ - Shakespeare.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var el= parentelement.replaceChild(newNode,oldNode);

    el contains the replaced oldNode, which can then be used as the
    newNode parameter.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot !
    /(bb|[^b]{2})/ - Shakespeare.


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
  •