1

I want to return the index of a given tr when I click on an element inside of a td.

The html

    <table class="tablesorter-js">
    <thead>
        <tr>
            <th class="txt-align-left>Builder</th>
            <th class=" txt-align-left " jobs">Current jobs</th>
            <tbody>
                <tr>
                    <td>some text <a class="link" href="#">A link</a>

                    </td>
                    <td>tome more text</td>
                </tr>
                <tr>
                    <td>some text <a class="link" href="#">A link</a>

                    </td>
                    <td>tome more text</td>
                </tr>
            </tbody>
</table>

the javascript

$('.link').click(function(e {
e.preventDefault();
console.log(oTable.fnGetPosition(this))
});

I know I can invoke the index of the current tr with this

var oTable = $('.tablesorter-js').dataTable();
$('.tablesorter-js tbody tr').click( function () {
    var pos = oTable.fnGetPosition(this)    
    // I want the position of the tr element when I click on .link              
    return pos;
});

How do I get that index of the current tr element when I click on .link?

3 Answers 3

1
$('.link').click(function(e) {
    e.preventDefault();
    console.log( $(this).closest('tr').index() );
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can get index of current <tr> element whose link is clicked with below jQuery code:

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

Fiddle Demo

Comments

0

Use index() jQuery function

var oTable = $('.tablesorter-js').dataTable();
$('.tablesorter-js tbody tr').click( function () {
    //var pos = oTable.fnGetPosition(this)       
    var idx = $(this).index(".tablesorter-js tbody tr");     
    return idx;
});

$('.link').click(function(e) {
    e.preventDefault();
    console.log( $(this).closest('tr').index(".tablesorter-js tbody tr") );
});

For more details .index() JQuery

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.