Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  81] [ 2]  / answers: 1 / hits: 27932  / 12 Years ago, tue, april 3, 2012, 12:00:00

What is the jQuery equivalent of querySelector? The only way I've found so far is to select all then pick the first selection:



$(selectorString)[0]


With the above expression, is jQuery smart enough to stop after finding the first match?



Update: @Mutnowski suggested using eq() and first, but after reading the jQuery documentation those two methods seem to have the same drawback: jQuery will first get all matches, then only filter out the first element.


More From » jquery

 Answers
11

So it seems that there is no equivalent of querySelector in jQuery, or in other libraries I've looked at.



The workaround is to select all matching elements (equivalent to querySelectorAll) then filter out the first one. This can be done using [0] (returns a html node) or as suggested by @Mutnowski with eq(0) or first (returning a jQuery object).


[#86439] Monday, April 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyla

Total Points: 77
Total Questions: 108
Total Answers: 111

Location: Grenada
Member since Mon, May 8, 2023
1 Year ago
;