With Jquery, I need to select just the first "n" items from the page, for example the first 20 links instead of selecting all of them with the usual
$("a")
Sounds simple but the jQuery manual has no evidence of something like this.
The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).
In jQuery, you can select multiple elements by separate it with a comma “,” symbol. In above case, select all elements that have a class name of “class1” and “class2”, and id of “id1”.
$(document) wraps a jQuery instance around the document object. ( $ is just an alias for jQuery .)
The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);
You probably want to read up on slice. Your code will look something like this:
$("a").slice(0,20)
Use lt pseudo selector:
$("a:lt(n)")
This matches the elements before the nth one (the nth element excluded). Numbering starts from 0.
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