.first()S2C 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() Examplego to top of page 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:


go to home page Homepage go to top of page Top