in Jquery why it won't alert each one in while loop?
<body>
<div id="div1">1</div>
<div id="div2">2</div>
<div id="div3">3</div>
<script>
$(document).ready(function(){
var boxNumber=1;
while(boxNumber<=3){
$('#div'+boxNumber).click(function(){
alert(boxNumber);
});
boxNumber++;
}
});
</script>
</body>
I expected the code to alert its box number when i click on it, but it
alerted number 4 instead. how do i fix it? Thanks
No comments:
Post a Comment