jquery - Counting classes up with each -


i trying give each .scan new class increasing count

what have tried using each function jquery , using $(this) $(this) allways contain class name every .scan changed.

$( ".scan" ).each(function() {     = 0;     $(this).addclass( "count"+i );     i++; }); 

the result

<div class="scan count0"></div> <div class="scan count0"></div> 

but want

<div class="scan count0"></div> <div class="scan count1"></div> 

here fiddle current state.

every tipp/help appreciated.

i.e. because defined inside each method.you need set value outside each method:

var = 0; $( ".scan" ).each(function() {    $(this).addclass( "count"+i );   i++; }); 

however can use default argument parameter index of each function give incremenetal class suffix:

$(".scan").each(function(i){   $(this).addclass("count" + i); }); 

working demo .each

or use callback function of addclass() method:

$(".scan").addclass(function(i){   return "count" + i; }); 

working demo .addclass


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -