.eq() JQ Home  <<  Filtering  <<  .eq()

Filter elements.

Description

The .eq() method is used to reduce the matched set to the element at the specified index.

Syntax

Signature Description
.eq( index )Reduce the matched set to the element at the specified index.
.eq( -index )Reduce the matched set to the element at the specified index, counting backwards from the last element in the set.

Parameters

Parameter Description Type
indexAn integer indicating the 0-based position of the element.Number
-indexAn integer indicating the position of the element, counting backwards from the last element in the set.Number

Return

A jQuery object containing the filtered element.

.eq( index ) Example  Filtering  <<  Top

Reduce the matched set to the element at the specified index.

In the example we select the third 'td' element and change the background colour.

Table For Testing The .eq( index ) 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(){
  $('#btn6').on('click', function() {
    $('.testtable td').eq(2)
                      .css('backgroundColor', 'orange');
  });
});


Press the button below to action the above code:

.eq( -index ) Example  Filtering  <<  Top

Reduce the matched set to the element at the specified index, counting backwards from the last element in the set.

In the example we select the second 'td' element from the end and change the background colour.

Table For Testing The .eq( -index ) 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(){
  $('#btn7').on('click', function() {
    $('.testtable2 td').eq(-2)
                       .css('backgroundColor', 'orange');
  });
});


Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 1 - Filtering Elements