hi i've got small issue not sure how solve javascript/jquery. ive got several div classes want create loop add class on divs, without having add id on them manually add id or class through javascript code.
heres idea of mean:
<div></div><div></div> <div></div><div></div> <div></div><div></div> <div></div><div></div> this have lets 2 divs row. want a class added in way make this:
<div class="green"></div> <div></div> <div></div> <div class="green"></div> <div class="green"></div> <div></div> <div></div> <div class="green"></div> so guessing sort of loop every 2 divs repeat in reverse.
basically want zig-zag.
there no need of loops. can use :nth-child selector follow:
$('div:nth-child(4n+1)').addclass('green'); // selects 1, 5, 9, 13, ... $('div:nth-child(4n)').addclass('green'); // selects 4, 8, 12, 16, ... here pure css demo.
body { width: 120px; } div { height: 50px; width: 50px; background: red; margin: 5px; float: left; } div:nth-child(4n+1) { background: green; } div:nth-child(4n) { background: green; } <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>
Comments
Post a Comment