How can I add a class to the first and second td in each tr?
<div class='location'>
<table>
<tbody>
<tr>
<td>THIS ONE</td>
<td>THIS ONE</td>
<td>else</td>
<td>here</td>
</tr>
<tr>
<td>THIS ONE</td>
<td>THIS ONE</td>
<td>else</td>
<td>here</td>
</tr>
</tbody>
</table>
</div>
For the first td, this does nothing?
$(".location table tbody tr td:first-child").addClass("black");
Can I also use second-child?
jQuery: code to get TD text value on button click. text() method we get the TD value (table cell value). So our code to get table td text value looks like as written below. $(document). ready(function(){ // code to read selected table row cell data (values).
grab the second child: $(t). children(). eq(1);
$(".location table tbody tr td:first-child").addClass("black");
$(".location table tbody tr td:nth-child(2)").addClass("black");
http://jsfiddle.net/68wbx/1/
To select the first and the second cell in each row, you could do this:
$(".location table tbody tr").each(function() {
$(this).children('td').slice(0, 2).addClass("black");
});
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