![]() Topic awaiting preservation: Reverse list order? (Page 1 of 1) |
|
---|---|
Maniac (V) Inmate From: California |
![]() Okay, so here's the deal. I've just started learning about DOM and JavaScript, and I figure the best way to learn is by doing. The thing is, I have no idea what I'm doing, so that's the part I'm going to need help with. code: <ul class="reversethis"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
code: <ul class="reversethis"> <li>Item 3</li> <li>Item 2</li> <li>Item 1</li> </ul>
|
Nervous Wreck (II) Inmate From: Here, there and everywhere |
![]() |
Maniac (V) Inmate From: California |
![]() Erm, yeah, that's along the lines of what I was thinking as well, but as I said, I know no JavaScript at all. A code example would be very helpful... |
Obsessive-Compulsive (I) Inmate From: |
![]() Enter this code into your JavaScript section: code: function reverseList(id) { c=document.getElementById(id); el=c.lastChild.previousSibling; p=el.previousSibling; while(el) { c.appendChild(el); el=p; p=el.previousSibling; } }
code: <ul id="myList" class="reversethis"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
code: <a href="javascript: reverseList('myList');">Click to Reverse</a>
|
Maniac (V) Inmate From: California |
![]() Great, that code works like a charm. But suppose I wanted to run the script as soon as the page loaded, and not when the user clicked on a link. How would I set that up? Sorry to inundate you with all these newbie questions... |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
![]() <body onload="reverseList('mylist');" > |
Maniac (V) Inmate From: California |
![]() Ay, thanks. That does it. |
Obsessive-Compulsive (I) Inmate From: |
![]() If you want the list reversed as soon as the page loads, why not generate it in reverse order in the first place rather than relying on JavaScript? |
Maniac (V) Inmate From: California |
![]() Heh, good question. It's really just a hack to circumvent some stupid limitation of the content management system I'm using (names omitted to protect the innocent |