Topic awaiting preservation: Reverse list order? (Page 1 of 1) |
|
---|---|
Maniac (V) Inmate From: California |
posted 07-20-2005 03:01
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 |
posted 07-20-2005 17:51 |
Maniac (V) Inmate From: California |
posted 07-20-2005 18:21
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: |
posted 07-20-2005 23:09
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 |
posted 07-20-2005 23:46
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 |
posted 07-21-2005 02:43
<body onload="reverseList('mylist');" > |
Maniac (V) Inmate From: California |
posted 07-21-2005 03:23
Ay, thanks. That does it. |
Obsessive-Compulsive (I) Inmate From: |
posted 07-22-2005 09:59
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 |
posted 07-23-2005 03:59
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 ). Luckily, I switched to another CMS just today, and no longer need this code. Yay. |