I have a few div with id(div1,div2,div3,div4). I want to know which div is selected in order to replace the previous selected div (default selected div is div1).
jQuery:
var current;
for(var i = 2; i <= 10; i++) {
if(current == "")
current = "#div1";
$("#div" + i).click(function(e) {
alert(i);
$(current).replaceWith($('#div' + i));
$('#div' + i).attr('id',current);
current = "#div" + i;
alert(current);
});
}
However when i do this, the loop do run through 1-10 BUT when i alert i inside the click function, it return me 11! How can number 11 be inside the loop!Can you please correct me?