I have several elements that I want to use nth-child
to apply a background-color
to. The issue is that I need to be able to hide any of these elements on the fly using javascript. I thought I could keep the alternating colors by adding/removing an allow
class, and applying nth-child
to that, but to no avail. I have an example going in a jsfiddle.
http://jsfiddle.net/tL40wz03/
Thanks for your help!
HTML
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item allow"></div>
<div class="item"></div>
<div class="item allow"></div>
<div class="item"></div>
<div class="item allow"></div>
CSS
div {
display: block;
height: 100px;
width: 100px;
}
div.allow{
background: gray;
}
div:nth-child(even){
background: orange;
}
div:not(.allow){
display:none;
}