I am generating textboxes dynamically in JavaScript but when I try to console log the (before passing them in array to ajax post) I only get undefined
. I tried searching the web for how to do it, but nothing seemed be a "hit".
Here is the essential parts.
//textbox naming convention is "id"+intlaskubox and I can read them like
textbox = document.getElementById("id2");
//amount of textboxes generated
var generated = intlaskubox;
console.log(generated);
//this is ok (I can get the value of the box)
textbox = document.getElementById("id" + generated);
console.log(textbox.value);
// (here I get undefined times the amount of boxes generated)
var i = 0;
while (i < generated) {
cu[i] = document.getElementById("id" + i);
console.log(cu[i].value);
i++;
}
I have tried Firebug and normal console log, but it seems I am doing something wrong.
Have you forgotten to initialize the array c
? Try this:
var i = 1, c = [],generated=5,inputRes=document.getElementById("res");
while (i <= generated) {
c[i] = document.getElementById("id" + i);
inputRes.value+=c[i].value;
i++;
}
because value not set in DOM
you change like this
while (i < generated ) {
cu[i] = document.getElementById("id"+i);
console.log(cu[i].getAttribute("value"));
i++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.