1

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.

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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+" ";
}

Comments

0

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

Comments

0

HTML element id should be unique.I suppose that u should give the element different ids first.Then get each value of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.