Why does this work, but the second example does not?
First one works.
function someFunk() {
$('.listOne li').filter(':odd').css('background-color', '#FFFFFF');
$('.listOne li').filter(':even').css('background-color', '#F0F0F0');
};
Second one does not work.
function someFunk() {
$('.listOne li').filter(':odd').css('background-color', '#FFFFFF').filter(':even').css('background-color', '#F0F0F0');
};
Can I not chain .filter() in jquery?
With jQuery, you can chain together actions/methods. Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.
Filtering works hand-in-hand with two other functional Array methods from ES5, map and reduce . And thanks to the ability to chain methods in JavaScript, you can use this combination to craft very clean code that performs some pretty complex functions.
you can use .end()
to return to the previous stack after DOM navigation methods.:
$('.listOne li').filter(':odd').css('background-color', '#FFFFFF')
.end().filter(':even').css('background-color', '#F0F0F0');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With