<table width="600px" id='testTable'>
<tr class="red"><td>this</td></tr>
<tr><td>1</td></tr>
<tr><td>1</td></tr>
<tr><td>1</td></tr>
<tr class="red"><td>1</td></tr>
<tr><td>1</td></tr>
<tr><td>1</td></tr>
<tr class="red"><td>this</td></tr>
<tr><td>1</td></tr>
<tr><td>1</td></tr>
<tr class="red"><td>1</td></tr>
<tr><td>1</td></tr>
<tr><td>1</td></tr>
</table>
.gray
{
background-color:#dddddd;
}
.red
{
color:Red;
}
$(function () {
$('#testTable tr.red:nth-child(odd)').addClass('gray');
//this should select tr's with text=this, but its not happening
});
i want to select all odds inside table which have class=red , but its not happening. please help
nth-child(odd) selects the odd members of the element’s parent not the odd members of the set returned by the selector.
You’re looking for this:
Edit: actually you want the even ones, i think. It’s a 0-based index. Demo