I would like to set the alignment of a table's headers based on the alignment of the first <td> in each respective column.
<table>
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Value One</td>
<td align="right">Value Two</td>
<td align="center">Value Three</td>
</tr>
<!-- more rows -->
</tbody>
</table>
In this example, the second <th> would get aligned to the right and the third <th> would get centered. The first <th> would not be affected. Any suggestions/examples of how to accomplish this with jQuery would be very appreciated.
Based on InfernalBadger's answer, this is what I ended up using. Turns out I needed to take into account the CSS text-align property as well as handling multiple tables.
function alignGridHeaders() {
$('table.grid thead th').each(function (index) {
var cell = $(this).parents('table:first').children('tbody tr:first td').eq(index);
var alignment = cell.css('text-align');
if (!alignment)
alignment = cell.attr('align');
if (alignment)
$(this).css('text-align', alignment);
});
}