Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery index() in relation to all visible siblings

I'd like to fetch the index of an element in relation to all visible siblings.

A td.index() does fetch the index of the td to all other td siblings.

But what when some of those TDs are set to display:none. I want to exclude them when calculating the index. td.index(':visible') does not seem to work.

like image 244
Zardoz Avatar asked Jun 27 '11 16:06

Zardoz


Video Answer


1 Answers

var $td = $("#theTD")

$td.siblings(":visible").andSelf().index($td);

The above should do what you're asking. Basically get the set of elements you want to search within and then get the index of your element within them.

http://jsfiddle.net/3NYY9/

EDIT: As of jquery 1.8 andSelf has been deprecated and addBack should be used in its place:

var $td = $("#theTD")

$td.siblings(":visible").addBack().index($td);

http://jsfiddle.net/3NYY9/45/

like image 53
James Montagne Avatar answered Dec 02 '22 02:12

James Montagne