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] );
Share:
10,957
Garry
Author by

Garry

Updated on June 14, 2022

Comments

  • Garry
    Garry almost 2 years

    How we can select first div of page using jquery ?

  • BetterTeng
    BetterTeng about 5 years
    var 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.