I have a table cell in the footer that allows the user to turn on row coloring:
$('#highlight').click(function() {
$(this).parents('table').RowColors();
})
// From Chapter 7 of Learning jQuery
$.fn.RowColors = function() {
$('tbody tr:odd', this).removeClass('even').addClass('odd');
$('tbody tr:even', this).removeClass('odd').addClass('even');
return this;
};
Q: How do I write a selector that says: IF there is at least 1 row with class=”even”, then remove both “even” and “odd” ELSE execute the RowColors function.
My advice would be to do it slightly differently. Have just one class with the default state being the other. So:
and then this is as simple as:
or more specifically:
Note: avoid using
:oddand:even. They usually don’t mean what you think they mean.:nth-child(odd)and:nth-child(even)tend to be what you really mean.I would probably write something like:
Put it into a separate function if you wish.
Edit: to check whether something is empty:
jQuery objects support the
lengthproperty and thesize()method, which do the same thing.