views:

63

answers:

2

I've got multiple divs, each with an ordered list (various lengths). I'm using jquery to add a class to each list item according to its index (for the purpose of columnizing portions of each list). What I have so far ...

<script type="text/javascript">

/*    Objective: columnize list items from a single ul or ol in a pre-determined number of columns
    1. get the index of each list item
    2. assign column class according to li's index
*/
$(document).ready(function() {

    $('ol li').each(function(index){
        // assign class according to li's index ... index = li number -1: 1-6 = 0-5; 7-12 = 6-11, etc.
        if ( index <= 5 )    {
            $(this).addClass('column-1');
            }
        if ( index > 5 && index < 12 )    {
            $(this).addClass('column-2');
            }
        if ( index > 11 )    {
            $(this).addClass('column-3');
            }

        // add another class to the first list item in each column
        $('ol li').filter(function(index) {
            return index != 0 && index % 6 == 0;
            }).addClass('reset');

    });    // closes li .each func
});    // closes doc.ready.func

</script>

... succeeds if there's only one list; when there are additional lists, the last column class ('column-3') is added to all remaining list items on the page. In other words, the script is presently indexing continuously through all subsequent lists/list items, rather than being re-set to [0] for each ordered list.

Can someone please show me the proper method/syntax to correct/amend this, so that the script addresses/indexes each ordered list anew?

many thanks in advance.

shecky

p.s. the markup is pretty straight-up:

<div class="tertiary">
 <h1>header</h1>
 <ol>
  <li><a href="#" title="a link">a link</a></li>
  <li><a href="#" title="a link">a link</a></li>
  <li><a href="#" title="a link">a link</a></li>
 </ol>
</div><!-- END div class="tertiary" -->
+2  A: 

This will iterate over each OL, but once at a time:

// loop over each <ol>
$('ol').each(function(olIndex){

    // loop over each <li> within the given <ol> ("this")
    $(this).find('li').each(function(liIndex){
        // do your <li> thing here with `liIndex` as your counter
    });

});

As for all that stuff in the middle, you might be able to improve it with some nicer selectors:

$('ol').each(function(){

  $(this).find('li')

     .filter(':lt(6)').addClass('column-1')            // <li> 1-5
       .filter(':first').addClass('reset').end().end() // <li> 1
     .filter(':gt(5):lt(12)').addClass('column-2')     // <li> 6-11
       .filter(':first').addClass('reset').end().end() // <li> 6
     .filter(':gt(11)').addClass('column-3')           // <li> 12+
       .filter(':first').addClass('reset');            // <li> 12

});

Of course if we're making columns here, maybe we should be getting these counts dynamically?

$('ol').each(function(){

  var $lis = $(this).find('li');
  var len = $lis.size();
  var colLen = Math.ceil(count / 3);

  // and so on with the filter stuff with 

});
Michael Haren
Thanks Michael! I got the results I want by adding the ('ol').each func. With that success, I can move on to the improvements you've suggested - I'll need a minute for those to sink in ... Cheers!
shecky
+1  A: 
$('ol').each(function(){
  $(this).find('li').each(function(index){
    // assign class according to li's index ... index = li number -1: 1-6 = 0-5; 7-12 = 6-11, etc.
    if ( index <= 5 )    {
        $(this).addClass('column-1');
        }
    if ( index > 5 && index < 12 )    {
        $(this).addClass('column-2');
        }
    if ( index > 11 )    {
        $(this).addClass('column-3');
        }
  }).filter(function(index) {
        return index != 0 && index % 6 == 0;
  }).addClass('reset'); // Closes li each and filter
}); // closes ol each
Matthew Flaschen
Cheers Matthew!
shecky