Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery equivalent of querySelector

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.

like image 457
Christophe Avatar asked Feb 02 '23 06:02

Christophe


1 Answers

You want .eq(index) to get an index

$("td").eq(2)
$("td:eq(2)")

http://api.jquery.com/eq/

If you want just the first use .first()

$("tr").first()
$("tr:first")

http://api.jquery.com/first/

like image 67
Mark U Avatar answered Feb 11 '23 07:02

Mark U