Is this an even or odd element? Is this an even or odd element? jquery jquery

Is this an even or odd element?


The callback to .each is passed the element's index and the element:

$('div').each(function(i, el) {   // As a side note, this === el.   if (i % 2 === 0) { /* we are even */ }   else { /* we are odd */ }});


$('div').each( function(index) {   //do something different based on whether even or odd div   if (index % 2 == 0) {}  // even   else {} // odd});


If you know that all of the elements are children of the same parent, you can use the index provided by each

$('div').each( function(index) {    if (index%2 == 0) {}    else {}});

otherwise, use the index function which will calculate the index of the element among its siblings.

$('div').each( function() {    if ($(this).index()%2 == 0) {}    else {}});