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?
However, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element(s). Tip: This way, browsers do not have to find the same element(s) more than once. To chain an action, you simply append the action to the previous action.
You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.
jQuery :gt() Selector The :gt() selector selects elements with an index number higher than a specified number. The index numbers start at 0. This is mostly used together with another selector to select the last elements in a group (like in the example above).
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/>
I suggest you use the slice() method instead.
http://docs.jquery.com/Traversing/slice#startend
$('table tr').slice(2, 5).addClass("something");
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