Chaining jQuery selectors :lt and :gt

glmxndr picture glmxndr · Jul 16, 2009 · Viewed 21.6k times · Source

I have a table with more than 9 rows.

If I do this : $('table tr:gt(3):lt(6)'), shall I receive 3 or 6 elements at the end, and why? Are all selectors applied to the same primary selection, or are they successively applied on different selections?

Answer

Blixt picture Blixt · Jul 16, 2009

They're applied sequentially, so first you will filter away the first four elements (:gt(3)), then you will filter away all elements after the sixth (:lt(6)) element of the already filtered set.

Imagine this HTML:

<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>
<br/><br/>

Then do the following jQuery:

$('br:gt(3):lt(6)').addClass('sel');

You will now have:

<br/><br/>
<br/><br/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br class="sel"/><br class="sel"/>
<br/><br/>