javascript - Why nth-child to add style to alternate visible element not working? -


i working on adding style alternate visible elements. thought of using nth-child(2n+1) idea, apparently doesn't works!. boil down problem, below sample:

html:

<div class='hide find'>test</div> <div class='hide find'>test</div>  <div class='find'>test</div>  <div class='hide find'>test</div>  <div class='find'>test</div>  <div class='hide find'>test</div>  <div class='hide find'>test</div>  <div class='find'>test</div>  <div class='hide find'>test</div>  <div class='find'>test</div> 

css:

.hide{     display:none; } .alternate{     background-color:grey; } 

jquery:

$('.find:visible:nth-child(2n+1)').addclass('alternate');    //this not working! why? 

i not sure of reason of failure. although, did workaround on problem , created function, works, smooth , better, if problem can resolved using above approach. below workaround made work:

function addalternatestyle(){ var alt= true;     $('.find:visible').each(function(){         if(alt){             alt=!alt;             $(this).addclass('alternate');         }         else{             alt=!alt;         }     }); } 

any appreciated.

you can try this:-

$('.find:visible:even').addclass('alternate'); 

demo


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -