1

I want to get indexing of match element only. Like I have '.get' class in few TR. I want to get indexing based on '.get' class for instance. fiddle

.get 0
.get 1
.get 2


<table>
    <tr class="get"><td>view</td></tr>   
    <tr><td>view</td></tr>   
    <tr class="get"><td>view</td></tr>   
    <tr><td>view</td></tr>   
    <tr><td>view</td></tr>   
    <tr><td>view</td></tr>   
    <tr class="get"><td>view</td></tr>   
</table>

$('tr.get').click(function(){
    alert($(this,'.get').index())
})
1
  • Your fiddle is empty :) Commented Jun 30, 2014 at 13:02

3 Answers 3

3

.index( element )

If a selector string is passed as an argument, .index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector.

Use

$('.get').index(this);

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Exactly what I want
1

Alternatively, you can directly attach the index in the handler. It will fail if you have dynamic data, but it will be much faster if you have a lot of data since it does not recompute the index each time.

$('.get').each(function(index) {
    $(this).click(function() {
        alert(index);
    })
})

DEMO

Comments

0

jsFiddle

JS

$('.get').click(function () {
    alert($(this, '.get').index())
})

HTML

<table>
    <tr class="get">
        <td>view</td>
    </tr>
    <tr>
        <td>view</td>
    </tr>
    <tr class="get">
        <td>view</td>
    </tr>
    <tr>
        <td>view</td>
    </tr>
    <tr>
        <td>view</td>
    </tr>
    <tr>
        <td>view</td>
    </tr>
    <tr class="get">
        <td>view</td>
    </tr>
</table>

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.