My problem is that I have a table, but I only want a subset of the rows, from first index to last index. I thought you could do it like this:
$('table tr:gt(2):lt(5)');
I thought it would give you only rows #3 and #4 but it ends up giving you more than that. How can I tell the selector to choose only rows #3 and #4?
To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.
slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument. The supplied start index identifies the position of one of the elements in the set; if end is omitted, all elements after this one will be included in the result.
Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.
Use a comma to union multiple queries: var tds = $(this). find('input, textarea'); You can also use :input as a selector, but it's not as efficient and may also include some things you'd rather not include.
You're pretty close, but the problem in your selector is the :lt(5)
filter.
You would want something like:
$('table tr:gt(2):lt(2)');
The difference is that by the time the lt()
filter is applied, the first three elements were already removed from the set (by the gt()
filter). So this will grab the 3rd and 4th element (zero-indexed), instead of the 3rd through the 8th.
How about $('table tr').slice(2, 4)
Since JavaScript arrays are 0 indexed, that will give you the third and fourth row of the table. Slice will still return a jQuery wrapped (sub)set, so it's the same end result.
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