I decide to move my (not so complex) app from Firebase.com to Parse.com. So my previous snippet in DOM that worked fine since yesterday to dynamically create N forms doesn't pass, anymore, the parameters correctly.
Here my snippet (i cut a lot of repetitive istructions):
function displayForms() {
while (divBot.hasChildNodes()) {
divBot.removeChild(divBot.lastChild);
}
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
// Ok suppose that my results was something like
// Position 0. ID = "Tom" and other things
// Position 1. ID = "Mark" and other things
// Position 2. ID = "Ivan" and other things
var object = results[i];
var id = object.id;
var form = document.createElement("FORM");
var fieldset = document.createElement("FIELDSET");
var inputId = document.createElement("INPUT");
inputId.setAttribute("type", "hidden");
inputId.value = id;
fieldset.appendChild(inputId);
var buttonDel = document.createElement("INPUT");
buttonDel.setAttribute("type", "button");
buttonDel.value = "Elimina";
console.log(id);
// HERE i have "Tom" at first iterate
// "Mark" at second and "Ivan" at third
// I also printed "i" and i have 0, 1, 2
buttonDel.addEventListener("click", function() {
// HERE i have "Ivan" all the times
// I also printed "i" and i have always 2
// So i am passing always the last value
del(inputId.value);});
fieldset.appendChild(buttonDel);
form.appendChild(fieldset);
divBot.appendChild(form);
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);}
});
}