Some ordered lists are better presented in descending order. For example, my publications list works better in reverse-chronological order so that the most recent work appears at the top of the list, and is numbered highest-to-lowest.
Ordered lists in HTML are created using the ol element, but there’s no way to present a list in descending order. (It’s in the HTML5 spec, but might be a while before it’s generally usable.)
The JavaScript solutions I found to achieve this were all a bit rubbish—adding spurious numbers to the margin and generally messing up the semantics of the list. So I wrote a quick function that finds each ol element on the page that has the class “reversed”, and changes the “value” attribute of each li to the number it would have in a native reversed-order presentation. It’s not semantically perfect, but it degrades gracefully and will do until browsers support the HTML5 incarnation of ol. The JavaScript looks like this:
function reverse_lists() {
var olists = document.getElementsByTagName('ol');
for (var i = 0; i < olists.length; i++) {
if (!olists[i].className.match(/\breversed\b/))
continue;
var items = olists[i].getElementsByTagName('li');
for(var j = 0; j < items.length; j++) {
items[j].setAttribute("value", items.length - j);
}
}
}
You can download a .js file with this function and upload it to your server. Apply the code by calling the function after all of your list elements (the first script element can be put in the document’s head if you have control over that):
<ol class="reversed">
<li>Most recent item.</li>
<li>Less recent item.</li>
⋮
</ol>
<script type="text/javascript" src="reorder.js"></script>
<script type="text/javascript">
reverse_lists();
</script>
And in your place an empty space Has filled the void behind my face.
7 Responses to “Reverse-ordered HTML lists”
Comment feed for this post.
I tried this and it worked very well, except that for LONG lists the offset of the list numbers was a bit odd. The low-end items were ‘way out to the left margin, and the high-end ones crowded in too close (literally as shown, with the 3-digit numbers starting *further* to the right than the 1-digit numbers):
With regular (non-reversed) lists, this does not happen, and instead nicely indents the list numbers appropriately to the length (# of digits) in each list number, as follows:
§ Comment by Don Perlis on March 16th, 2009 at 1:28 am
Darn! The text box *formatted*my previous reply, thereby ruining my carefully designed examples. (Fixed. —Ed.) But my claims are correct, so you’ll have to imagine the item number indentations as I describe them. On the regular non-reversed list, just enough space is allowed on the left margin so that even the long-digit numbers have room, without crowding up against the item text,while shorter numbers are a bit further indented, so that the item number is always a fixed distance from the item text that is to its right.
But in the reverse mode, this spacing is all messed up.
§ Comment by Don Perlis on March 16th, 2009 at 2:16 am
Very helpful, thanks!
§ Comment by Tim Wright on September 1st, 2009 at 9:11 pm
Using this on my list of publications, works well. Thanks !
§ Comment by Minh Ha-Duong on December 1st, 2009 at 9:16 am
Working fine thanks a lot
§ Comment by R. Biswas on June 1st, 2010 at 10:07 am
Thanks! works great!
§ Comment by Tsekub on January 27th, 2011 at 8:42 pm
Works nice. Thanks!
§ Comment by Dan on January 10th, 2012 at 4:54 pm