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

Solution 1:

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());

Solution 2:

var $th = $("table thead tr th").eq($td.index())

It would be best to use an id to reference the table if there is more than one.

Solution 3:

You can do it by using the td's index:

var tdIndex = $td.index() + 1;
var $th = $('#table tr').find('th:nth-child(' + tdIndex + ')');