.first() JQ Home  <<  Filtering  <<  .first()

Filter first element.

Description

The .first() method is used to reduce the matched set to the first element.

Syntax

Signature Description
.first()Reduce the matched set to the first element.

Parameters

None

Return

A jQuery object containing the first element of the matched set.

.first() Method Example Filtering  <<  Top

Reduce the matched set to the first element.

In the example below we select all 'td' elements within the table with the class of 'testtable' and then reduce the matched set to the first element.

Table For Testing The .first() Signature
Table Row 1, Table Data 1 Table Row 1, Table Data 2
Table Row 2, Table Data 1 Table Row 2, Table Data 2



$(function(){
  $('#btn1').on('click', function() {
    $('.testtable td').first()
                      .css('backgroundColor', 'orange');
  });
});


Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 1 - Filtering Elements