Selecting first div usin jquery
10,957
Solution 1
jQuery first select: http://api.jquery.com/first-selector/
The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.
Sample:
var div = $('div:first');
Fiddler example: http://jsfiddle.net/wBQfm/
Solution 2
Get the first using this:
var first_div = $( $('div')[0] );
Author by
Garry
Updated on June 14, 2022Comments
-
Garry almost 2 years
How we can select first div of page using jquery ?
-
BetterTeng about 5 yearsvar div = $('div').filter(":first"); actually achieves the best performance, since queries using :first cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method.