jQuery API

:first Selector

first selector

version added: 1.0jQuery(':first')

Description: Selects the first matched element.

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.

Example:

Finds the first table row.

<!DOCTYPE html>
<html>
<head>
  <style>

  td { color:blue; font-weight:bold; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<table>
    <tr><td>Row 1</td></tr>
    <tr><td>Row 2</td></tr>

    <tr><td>Row 3</td></tr>
  </table>
<script>$("tr:first").css("font-style", "italic");</script>
</body>
</html>

Demo:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for :first Selector below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • hc
    If you know there is only one element to be found, is it faster (in terms of performance) to use ':first' as part of the selector or not?

    For example, if your markup is this:

    <table>
    <tr><td>Row 1</td></tr>
    </table>

    And you want to get the first row, and you know there will always only be one row, is it faster to do:

    $('table > tr')

    or

    $('table > tr:first')

    Has anyone done speed tests to verify the answer?