02.06.2013 Views

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

jQuery Cookbook - Cdn.oreilly.com - O'Reilly

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

(function($){<br />

$(document).ready(function() {<br />

$("ul > li").each(function(i) {<br />

if (i % 2 == 1)<br />

{<br />

$(this).addClass("odd");<br />

}<br />

else<br />

{<br />

$(this).addClass("even");<br />

}<br />

});<br />

});<br />

})(<strong>jQuery</strong>);<br />

<br />

<br />

<br />

Family Members<br />

<br />

Ralph<br />

Hope<br />

Brandon<br />

Jordan<br />

Ralphie<br />

<br />

<br />

<br />

Figure 3-1 shows the code output.<br />

Figure 3-1. Code output<br />

As we iterate through each element, we are testing whether the current index,<br />

which is passed in as a single argument to the function when executed, modded by 2<br />

is equal to 1. Based on that condition, we either set one CSS class (.odd) or another CSS<br />

class (.even).<br />

3.1 Looping Through a Set of Selected Results | 55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!