I have multiple buttons with the ids "especifica-i-pj", where i goes from 1 to 4 and j goes from 1 to 3. The idea is to assign an onclick event to each one of them, without having to declare 12 functions. I thought this would solve my problem:
for ( i = 1; i <= 4; i++ ) {
for ( j = 1; j <= 3; j++ ) {
dom_id = "especifica-"+i.toString()+"-p"+j.toString();
document.getElementById(dom_id).onclick = function() {
console.log("you clicked ",dom_id)
ponder[i-1] = 0.1 + 0.05*(j-1);
console.log("ponder:",i,ponder[i-1])
$("#"+dom_id).addClass("active");
for ( k = 1; k <= 3 && k != j; k++ )
$("#especifica-"+i.toString()+"-p"+k.toString()).removeClass("active");
}
}
}
However each time I click any of the buttons, my console outputs that I clicked especifica-4-p3, the last one, and outputs ponder: 5 0.25, when i==5 should not be possible, as the outer for gets to i==4.
What can I do to make this work? I have been searching online but I have not found anything about this, or even declaring functions inside for loops. Thanks.
HTMLlook like? I'd be helpful to see that as well