Consider the following example, which should color every second line: ( live demo here )
JS:
$(function() { var wrapper = $("<div></div>") for (i = 0; i < 100; i++) { wrapper.append("<span></span>"); } $("body").append(wrapper); color_rows(); }); function color_rows() { $("span").each(function(i) { if (i % 10 >= 5) { $(this).css("background-color", "red"); } }); }
CSS
div { width: 450px; height: 400px; background-color: #ccc; overflow: auto; } span { display: inline-block; width: 50px; height: 50px; background-color: #777; margin: 0 30px 30px 0; }
Output:

As you can see, the color_rows() function assumes that there are 5 elements in the line. If, for example, I change the width of the div to 350px , the color_rows() function will not work correctly (i.e., it will not color every second line).
How can I fix color_rows() so that it works for each div width?
source share