I am looking to modify the left-margin value of various separate DIVs using JavaScript. The challenge is:
- I only want to use a single className, and
- I want the margin to increase by 100px for each instance of the class. This way, instead of all the DIVs overlapping, they will be evenly spaced out: the first at margin-left:0px, the second at margin-left:100px, the third at margin-left:200px, and so on.
Currently, I have code that applies the same margin-left to all DIVs.
<script>
b = document.getElementsByClassName('spacing');
for (i = 0; i < b.length; i++) {
b[i].style.marginLeft = "100px";
}
</script>
I am wondering if there is a way to have the JavaScript dynamically find each instance of the class sequentially and instead of just setting margin-left: 100px for all, to compute something like (margin applied to last instance of class + X) so that each of the 100 DIVs with the same className will have a unique marginLeft value?