Here i have written some code for getting values from dynamically created input box using javascript
Below is the Code for setting value:
for(var i=0;i<result.studentlist.length;i++){
var addtxt = document
.createElement("input");
addtxt.type = "text";
addtxt.name = "admissionno" ;
addtxt.id = "admissionno" ;
addtxt.value = result.studentlist[i].admissionno;
}
And getting purpose written below code:
var admissionNumber=document.getElementById("admissionno").value;
Actually two Admission numbers appended.
But, when am getting value at that time only first value is coming.
please give me an idea.
document.getElementById("admissionno")
will always provide you a single element since ids are unique and the function also returns a single DOM element.
You should instead add classes and use them like document.getElementByClassName("classname")
. It will return you a Node List through which you can iterate over.
The parameter id must be unique. A solution will be:
Generation:
for(var i=0;i<result.studentlist.length;i++){
var addtxt = document.createElement("input");
addtxt.type = "text";
addtxt.name = "admissionno" ;
addtxt.id = "admissionno"+i ;
addtxt.value = result.studentlist[i].admissionno;
}
Collecting data (assuming you want to put all together):
var inputs=document.getElementsByName("admissionno");
var admissionNumber="";
for (var j=0;j<inputs.length;j++) {
admissionNumber+=inputs[j].value+" ";
}
I would put the input elements in a form to get multiple values at the same time. Also, if I am not mistaken you are creating duplicate id's with your code. What you want is:
document.getElementsByName("admissiono")
This method returns you an array of elements which can be iterated over. Or you can access certain positions with:
document.getElementsByName("admissiono")[0].value
HTML元素ID应该是唯一的。我想你应该先给元素赋予不同的ID,然后获取它们的每个值。
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.