I want to assign an onclick event to the buttons which are created dynamically. The funtion is named myFunction and all the buttons call her but with differents argument. So I must use an anonymus function to assign the function. The problem is the assignment doesn't work. When I press any buton it calls the right function but with the last argument assigned.
html:
<div id="tbodyHT"></div>
javascript:
var contenu = "";
for (var i = 0; i < 7; i++) {
contenu = contenu + "<a href='#' class='button small green' id='btndltHT" + i + "'></a><br/>";
}
document.getElementById('tbodyHT').innerHTML = contenu;
for (var i = 0; i < 7; i++) {
document.getElementById('btndltHT' + i).onclick = function() { alert(i) };
}
code in JsFiddle, but it doesn't work because I don't know how to handle this site.
Thanks in advance
";` – IMTheNachoMan Jul 20 '16 at 16:22