Given the following table, how would I get the corresponding table header for each td element?
<table> <thead> <tr> <th id="name">Name</th> <th id="address">Address</th> </tr> </thead> <tbody> <tr> <td>Bob</td> <td>1 High Street</td> </tr> </tbody> </table>
Given that I currently have any of the td
elements available to me already, how could I find the corresponding th
element?
var $td = IveGotThisCovered(); var $th = GetTableHeader($td);
Go to Table Tools > Design on the Ribbon. In the Table Style Options group, select the Header Row check box to hide or display the table headers.
The TH and TD elements are used for table cells. TH is used for table header cells while TD is used for table data cells. This distinction gives user agents a means to render such cells distinctly, for instance by using a larger or heavier font for header cells.
Header cells are those that contain the information that is critical to understanding the raw data in a table. For example the number 210 is meaningless on its own, but becomes information if you know that it is the data for a) the number of properties in b) a given street.
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());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With