最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论