I want to setup a jquery function on each html element. Since each element has a unique id, I use a for loop:
function setupSwitch(temperatureNumArr){
var id = '';
for (var i = 0; i < temperatureNumArr.length; i++) {
id = 'temperature_switch_'+temperatureNumArr[i]
$(id, function(){
console.log(id);
$(id).change(function() {
alert(($(this).prop('checked')) + typeof(($(this).prop('checked'))))
});
});
}
the above code only prints the last id, and the on change function fails, I think it is caused by javascript function scope, but I am not sure how to pass id into the $(...) on change function.