There is such DOM structure:
<div class="info" id="step1">
<a href="#" id="next_step_1"> ok </a>
</div>
<div class="info" id="step2">
<a href="#" id="next_step_2"> ok </a>
</div>
<div class="info" id="step3">
<a href="#" id="next_step_3"> ok </a>
</div>
... (so on)
Div .info
has display:none
property (except 1), so what I want to achieve: when I click ok, this div fades out, next div fades in.
Of course, that's not DRY to write function to each div, so I tried this:
for (var i=1; i<5; i++){
$("body").delegate('#next_step_'+i, 'click', function(){
$("#step"+i).fadeOut();
$("#step"+i+1).fadeIn();
});
}
This code didn't work, and I tried:
for (var i=1; i<5; i++){
$("body").delegate('#next_step_'+i, 'click', function(){
alert("#step"+i)
});
}
And alert worked(so on delegate i
was 1) but alert was #step5
.
That confuses me, I supposed JS output 5 functions for each div... Help me get this around.