Performing an Action on a Subset of the Selected Set - J Query

You need to perform an action on a set of tags, but there is no way to isolate these tags from all the other tags on the page in a jQuery selection set.


We can use the slice() method to filter the selection set to a subset. We pass it a starting index value and an ending index value, then we can chain our action at the end:


Figure : shows the output.

The preceding example selects the subset starting at index 1 and ending before index 3 and wraps an italics tag around the subselection.

The jQuery method slice() takes a couple of options; the first is the starting index position, and the second argument, which is optional, is the ending index position. So, say you wanted all <P>tags except the first one; you could do $("p").slice(1), and it would start the selection at the second item and select the rest that is in the jQueryselection.
slice() also takes a negative number. If a negative number is given, it’ll count in from the selection’s end. So, $("p").slice(−1); will select the last item in the selection. Additionally, $("p").slice(1, −2); will start the selection at the second item and select to the second-to-last item.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

J Query Topics