I have multiple buttons on a page and want them to perform similar tasks. In PHP I can manage to do so, but in JavaScript I can't seem to get it working:
for (i= 0; i<5; i++){
$("#btn_"+i).click(function(){
alert(i);
});
}
The alert is always 5
My PHP solution is working fine, but I want to cut back on code written on the page...
<?php
for ($i= 0; $i<5; $i++){
?>
$("#btn_page-<?php print $i; ?>").click(function(){
$("#btn_"+i).click(function(){
alert("<?php print $i; ?>");
});
<?php
}
?>