<p>At work this past week, a client requested something that I wasn’t sure how to achieve: a reversed ordered list. So basically, they wanted to have a top 10 list, with the first item being numbered 10 and the last 1.</p>

<p>If you want to <a href="//assets.trevor-davis.com/play/reversed-ordered-list-with-jquery/">skip ahead to the demo</a>, go ahead.</p>

<p><a href="//assets.trevor-davis.com/play/reversed-ordered-list-with-jquery/" class="more-link">View Demo</a></p>

<p>Now sure, I could have just written the numbers in as text, but it’s a list, and it’s ordered! Here is an example of what the client wanted:</p>

<p>10. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />
9. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />
8. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />
…<br />
1. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<p>After some digging around, I found out that you can add the <strong>value attribute</strong> to each list item to specify the number to display. So basically, I just needed to code my list like this:</p>

<div class="codeblock"><code>
&lt;ol&gt;<br /> &lt;li value="10"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &lt;li value="9"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &lt;li value="8"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &amp;hellip;<br /> &lt;li value="1"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br />&lt;/ol&gt; 

</code></div>

<p>Pretty straightforward, but also pretty ugly. So what happens when the client decides they want to insert another bullet in the middle of the list? Yep, that’s right, they would have to adjust the numbering for the rest of the list.</p>

<p>Pretty crappy, so let’s use the powerfulness of jQuery to make this a little better.</p>

<h2>The Markup</h2>
<p>So just like before, we just want to create an ordered list, but this time, we will leave off the value attribute. We are also going to add a class to the ordered list that we will use as a hook to add in the jQuery functionality:</p>

<div class="codeblock"><code>
&lt;ol class="reversed"&gt;<br /> &lt;li&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &lt;li&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &lt;li&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br /> &amp;hellip;<br /> &lt;li&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/li&gt;<br />&lt;/ol&gt; 

</code></div>

<h2>The jQuery</h2>
<p>Mapping out the code a bit: we want to select each element with a class of reversed, and count the total number of children list items. Then, we want to loop through the children, and update the value attribute to be the value of the total number of children minus a counter variable that we started at zero and increment each time through the loop.</p>

<p>That seems relatively straightforward, so I’ll just show the code that I wrote:</p>

<div class="codeblock"><code>
$(document).ready(function() {<br /> $('.reversed').each(function() {<br />  var $children = $(this).children('li');<br />  var totalChildren = $children.length;<br />  var start = 0;<br />  $children.each(function() {<br />   $(this).val(totalChildren - start);<br />   start++;<br />  });<br /> });<br />}); 

</code></div>

<p><a href="//assets.trevor-davis.com/play/reversed-ordered-list-with-jquery/" class="more-link">View Demo</a></p>

<p>It looks like with HTML 5, we get a <a href="http://blog.whatwg.org/reverse-ordered-lists">reversed attribute</a> that accomplishes the same thing, but until it’s fully supported, this quick &amp; easy solution seems to work.</p>

<h2>On a Personal Note</h2>
<p>I have been severely neglecting my blog, but for good reason. I have been teaching some intro to web development courses at <a href="http://www.cdiabu.com/">CDIABU</a> in Georgetown. Also, I will be starting a new job on December 1st. I will be joining the amazing team at <a href="http://www.viget.com/">Viget Labs</a> as their new front-end developer.</p>