I'm trying to initialize a bunch of jquery functions using a foor loop. I use the for loop's counter in each iteration to call the function on a row in a table.
But it doesn't works. I think it's because the counter doesn't exists within the function. This is because I have already tried to run my function hard-coding the places where the counter should be, with, say, 0, and my function works as intended on row 0 of my table. When I leave the counter in place it throws me an error, says that the html elements I get by Id are null.
Is there something I'm missing? How do I pass the counter into a function in javascript?
Here's my code:
var typingTimer; //timer identifier
var doneTypingInterval = 400; //time in ms (5 seconds)
var i
for (i = 0; i < 10; i++) {
//on keyup, start the countdown
$('#precios-'.concat(i,'-precio')).keyup(function(i){
clearTimeout(typingTimer);
if (document.getElementById('precios-'.concat(i,'-precio')).value) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});
//user is "finished typing," do something
function doneTyping(i) {
var it = i
$.getJSON('./_revisarCaptura', {
unidades: $($($('#precios-'.concat(it,'-precio')).parents()[1]).siblings()[6]).html(),
ultimoPrecio: $($($('#precios-'.concat(it,'-precio')).parents()[1]).siblings()[7]).html().substring(1),
nuevoPrecio: document.getElementById('precios-'.concat(it,'-precio')).value,
nuevasUnidades: document.getElementById('precios-'.concat(it,'-precio')).value,
}, function(data) {
$('#precios-'.concat(it,'-precio')).css('backgroundColor', data.result)
})
return false
}
i++
}