How can I add a background-color to every second table row even some table rows are not displayed?
In my HTML is a simple table with some table rows. Just like that:
<label for="cb">hide blue</label>
<input id="cb" type="checkbox" onchange="hideBlue(this.checked)" />
<table>
<tr data-cat="red">
<td>red car</td>
</tr>
<tr data-cat="blue">
<td>blue jacket</td>
</tr>
<tr data-cat="red">
<td>red bull</td>
</tr>
<tr data-cat="red">
<td>red pen</td>
</tr>
</table>
As you can see there is also a checkbox which can toggle the "blue" category-rows. My CSS is looking like that:
tr:nth-child(odd) {
background-color:#aaa;
}
But now if someone hides the "blue" category (by adding a display:none
-class), the background colors are not alternate right anymore.
How can I prevent this failure?
I already tried to add :not(.displaynone)
to the css rule but this didn't work.
Here is a jsFiddle