JQuery - get the td index of a control's cell

** EDIT ** I'm really sorry. I need the td index, not the row index.

Using jQuery, how to get td index lblNamein?

       <table>
            <tr>
                <td>
                </td>
                <td>
                    <label id="lblName" />
                </td>
                <td>
                </td>
            </tr>
        </table>
+5
source share
4 answers

Update updated question:

$('#lblName').parent().index();

or

document.getElementById('lblName').parentNode.cellIndex

Original answer:

Use .index().

$('#lblName').closest('tr').index();

The method .index()has several different usage patterns.

This means that "take the first element corresponding to the selector and return its index based on a zero value among its siblings."


Or initially do it ...

document.getElementById('lblName').parentNode.parentNode.rowIndex

Or combine the two ...

$('#lblName').closest('tr')[0].rowIndex;
+18
source
var index = $('#lblName').closest('tr').index();
0
source
$("table tr").index($("#lblName").parents("tr:first"))
0

, .

index() parentUntill() TR TD.

example http://jsfiddle.net/ylokesh/bGvHZ/

$('#lblName').parentsUntil('tr').index()
0

All Articles