Darth Vader Darth Vader - 16 days ago 5
jQuery Question

Loop Attribute number increasing the value by 5 each time

I would like to add an attribute to the

div
below. However I want each value to increase by
5
. For some reason when I try the code below, it repeats the number rather than increasing it by
5
.

<div class="yo">TOOTO</div>
<div class="yo">TOOTO</div>
<div class="yo">TOOTO</div>
<div class="yo">TOOTO</div>


var text = "";
var i = 0.7;
while (i < 80) {
$('.yo').attr('wow', i)
i += 5
}

Answer
var text = "";
var i = 0.7;
var j = 0;
while (i < 80) {

 $('.yo').eq(j).attr('wow', i)
 j++;
 i+=5
}

Select one element at the time,$('.yo') returns an array.

EDIT

Consider replacing while(i < 80) with while($('.yo).eq(j)) if you want to set attribute in all the elements.

Comments