Xm7X
Xm7X

Reputation: 861

In jquery can I chain .filter twice?

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?

Upvotes: 5

Views: 1146

Answers (1)

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33870

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');

Upvotes: 17

Related Questions