Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get td cell index of a control

Tags:

jquery

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

Using jQuery, how do I get the index of the td lblName is in?

       <table>
            <tr>
                <td>
                </td>
                <td>
                    <label id="lblName" />
                </td>
                <td>
                </td>
            </tr>
        </table>
like image 765
tempid Avatar asked Feb 05 '12 16:02

tempid


1 Answers

Update for updated question:

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

or

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

Original answer:

Use .index().

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

The .index() method has a few different use patterns.

This one means "take the first element matched by the selector, and return its zero based index of its position among its siblings".


Or natively, do this...

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

Or combine the two...

$('#lblName').closest('tr')[0].rowIndex;
like image 187
3 revsuser1106925 Avatar answered Oct 28 '22 02:10

3 revsuser1106925