Samuel
Samuel

Reputation: 2156

Find index of TD within siblings

I'm refactoring the following code:

$(this).parent().find('td').index(this)

into this code:

$(this).siblings('td').index(this)

which baslically searches among the TDs of the current TR by accessing sibling TDs rather than going up to the TR and down again to the TDs.

But where the first code returns 19, which is correct, the second returns -1, which obviously isn't.

I'm puzzled...

Upvotes: 0

Views: 98

Answers (2)

Samuel
Samuel

Reputation: 2156

Got it!

$(this).siblings('td').addBack().index(this)

Siblings are actually all other TDs, so I have to include the current TD as well!

Upvotes: 1

Blazemonger
Blazemonger

Reputation: 92943

I believe what you wanted is:

$(this).siblings('td').addBack().index(this)

http://api.jquery.com/addBack

Upvotes: 3

Related Questions