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); });
or use callback function of addclass()
method:
$(".scan").addclass(function(i){ return "count" + i; });
Comments
Post a Comment