Abhishek Kumar
Abhishek Kumar

Reputation: 2276

Get class of forth cell adjacent to current cell

I am trying to get class of fourth cell using eq() selector. Its working without eq selector like this :

 alert($cell.closest( "td" ).next().next().next().attr("class"));

I tried using multiple variation of eq() but it's not working please help. None of the below are working.

 $cell = $(this);
 alert($cell.find( "td" ).eq(3).attr("class"));
 alert($cell.closest( "td" ).eq(3).attr("class"));
 alert($cell.( "td:eq(3)" ).attr("class"));

Upvotes: 2

Views: 58

Answers (1)

Ram
Ram

Reputation: 144689

That's not how the eq method work.

Given a jQuery object that represents a set of DOM elements, the .eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set.

As the closest method returns one element, eq(3) returns an empty set in here. You can use the nextAll method for creating a set of next siblings. Then eq(3) will return the fourth element in that set:

$cell.closest("td").nextAll().eq(3).attr("class");

Please note that if this here refers to a td element then closest('td') does nothing.

Upvotes: 1

Related Questions