SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Moving an Element in the DOM

    say i have a list...

    Code HTML4Strict:
    <ul id="my-list">
         <li>Aligator</li>
         <li>Billy Goat</li>
         <li>Cat</li>
         <li>Dog</li>
    </ul>

    ...and i want to move Cat from spot 3 to spot 1.

    i can select the Cat item...

    Code JavaScript:
    var element = document.getElementById('my-list').getElementsByTagName('li')[2]; // selects the 3rd element in the list - Cat

    but now what? how can i tell this element to be higher/lower on the list?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var pa= element.parentNode;
    var sib=element.previousSibling;
    if(sib)pa.insertBefore(element,sib);

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    501
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, i had just come across that after searching a bit more on google. works great!

    this is the article i saw: http://www.webreference.com/js/colum...ertbefore.html


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
  •