I'm working with JavaScript and jQuery and I want to display a bunch of buttons.
These buttons get a name from a variable incremented in the loop and they should show this number.
The naming process works fine, but when I click on these buttons I get everytime the value 10. I know the reason for this output, but I don't know how I can get the real number of the button (I don't want to get the value from the button name). Is there a way to make the 'click' value 'static' ?
Here is a example code :
<script>
for(var i = 0; i < 10; i++) {
$(document.createElement('button'))
.html(i)
.click(function() {
alert(i);
})
.appendTo($(document.body));
}
</script>