dom - How to get values from dynamically created input box in javascript - Stack Overflow

Here i have written some code for getting values from dynamically created input box using javascriptBel

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 ing.

please give me an idea.

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 ing.

please give me an idea.

Share Improve this question asked Apr 11, 2014 at 12:03 user2996174user2996174 1,4815 gold badges15 silver badges20 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 1

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 element id should be unique.I suppose that u should give the element different ids first.Then get each value of them.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567087a4633460.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信