Let's say I have the following html:
<table>
<thead>
<tr>
<th class="alignRight">Header1</th>
<th>Header2</th>
<th class="alignLeft">Header3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row1</td> //Add class="alignRight"
<td>Row1</td>
<td>Row1</td> //Add class="alignLeft"
</tr>
<tr>
<td>Row2</td> //Add class="alignRight"
<td>Row2</td>
<td>Row2</td> //Add class="alignLeft"
</tr>
<tr>
<td>Row3</td> //Add class="alignRight"
<td>Row3</td>
<td>Row3</td> //Add class="alignLeft"
</tr>
</tbody>
</table>
If a TR in the THEAD contains the class "alignRight" or "alignLeft" I would like to apply the same class to the corresponding TD in the TBODY.
I'm using JQuery and have tried the following, but none of them seem to work properly:
$('tr').has('th.alignRight').addClass('th.alignRight');
$('tr').has('th.alignLeft').addClass('th.alignLeft');
$('thead.tr').has('th.alignRight').addClass('tbody.th.alignRight');
$('thead.tr').has('th.alignLeft').addClass('tbody.th.alignLeft');
if ($('thead.tr').has('th.alignRight')){
$('tbody.tr').addClass('th.alignRight');
}
if ($('thead.tr').has('th.alignRight')){
$('tbody.tr.td').addClass('alignRight');
}
Any ideas on what could be wrong?
UPDATE:
I would like to add that I already iterate through the table using a .each() loop. I only need to add the condition that if the current table header has that class, add that same class to the table cell. Adding in an extra iteration during the current iteration sounds like it would lead to performance issues. Is that true?
LOOP:
function(res) {
var tbl_body = "";
$.each(res, function () {
var tbl_row = "";
$.each(this, function (k, v) {
//Irrelevant code
tbl_row += "<td>" + v + "</td>";
})
tbl_body += "<tr>" + tbl_row + "</tr>";
})
$("#print").html(tbl_body);
},
//Other irrelevant code