The following code works fine:
$(document).on('click', "input[id='inputfield0']",
function() {
myFunction(0);
});
$(document).on('click', "input[id='inputfield1']",
function() {
myFunction(1);
});
$(document).on('click', "input[id='inputfield2']",
function() {
myFunction(2);
})
I want to remove the repetition, as follows:.
function oneCall() {
for (var i = 0; i < 2; i++) {
$(document).on('click', "input[id='inputfield"+i+"']",
function() {
myFunction(i);
});
}
}
function oneCall();
However, in the new function oneCall()
, each time myFunction()
is called within the loop, it is being passed a value of 2, rather than being incremented on each call.
What is the correct way to restructure this code?
Thanks