2

I'm working on a web platform that use a lot of tables to render on browser. So I need to use the index() of some elements to provide an interaction to the users.

But as I commented above when I try those tables, the index isn't working very well.

Details here: http://jsfiddle.net/gambin/JGWe3/3/

I tried to use another ways (without tables) and it's working without any problem!

Any suggestions?

3 Answers 3

2

You get the index with respect to its immediate parent, here your immediate parent is td and which is preceded by span so you get index 1, To get the index of each element with respect to parent table you need index of row enclosing subBullet class. You can get row index by following code, 0 index for first row and so on. Reading this on jquery Doc worth understanding how index() works.

Live Demo using table

$('.subBullet').click(function(){
    alert($(this).closest('tr').index());
})​

Live Demo using Div

$('.subBullet').click(function(){
    alert($(this).index());
})​
Sign up to request clarification or add additional context in comments.

4 Comments

or use parent: alert($(this).parent().parent().index());
To explain this a bit more for the QA: In the original code, you were counting the index of that div inside that TD. Since the TD only contained 1 div, you would always get 1 back.
Thanks for demos and comments!!
You need to understand how index and selector work, this could help you api.jquery.com/index, I Updated my answer as well.
0

Try this

$('.subBullet').click(function(){
    alert($(this).closest("tr").index());
})​

Comments

0

If you are after the index of the clicked element then it would be:

$('.subBullet').click(function(){
    alert($('.subBullet').index($(this)));
})​

http://jsfiddle.net/wGWCT/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.