<table>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>ccc</td><td>aaa</td><td>aaa</td><td>bbb</td><td>aaa</td><td>aaa</td></tr>
</table>
table td {
background-color: green;
padding: 5px;
border: 1px solid blue;
}
.red {
background-color: red;
}
LIVE: http://jsfiddle.net/zCduV/1/
How can i add class .red with jQuery for second column in this table (in this example this is there where in td is ccc)?
This maybe?
// selects both table header and table data cells from the second column of the table
$('table th:nth-child(2), table td:nth-child(2)').addClass('red');
http://jsfiddle.net/tdTkQ/
By using the jQuery nth-child selector:
$('td:nth-child(2)').addClass('red');
Reference: http://api.jquery.com/nth-child-selector/
$('td:nth-child(2)').addClass('red');
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