GenericTypeTea GenericTypeTea - 1 year ago 87
Javascript Question

How can I get the corresponding table header (th) from a table cell (td)?

Given the following table, how would I get the corresponding table header for each td element?

<th id="name">Name</th>
<th id="address">Address</th>
<td>1 High Street</td>

Given that I currently have any of the
elements available to me already, how could I find the corresponding

var $td = IveGotThisCovered();
var $th = GetTableHeader($td);

Answer Source
var $th = $td.closest('tbody').prev('thead').find('> tr > th:eq(' + $td.index() + ')');

Or a little bit simplified

var $th = $td.closest('table').find('th').eq($td.index());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download