0

I am using the code from this link. http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/

var counter = 1;
var limit = 3;
function addInput(divName){
    if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + "    inputs");
     }
     else {
          var newdiv = document.createElement('div');
           newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input    type='text' name='myInputs[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
         }
 }

How do I get the value from the text input?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
user2738201
  • 53
  • 1
  • 7
  • If you add a relevant piece of your code to your question, you will get an answer. Do just like you did a few month ago http://stackoverflow.com/questions/26695254/java-printf-with-date-and-month . And there is a [badge](http://stackoverflow.com/help/badges/10/scholar) if you accept answers. – francis Mar 03 '15 at 20:06
  • I've added the code. Sorry about that. – user2738201 Mar 03 '15 at 20:09

2 Answers2

0

add id attr into input:

newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input    type='text' name='myInputs[]' id='"+counter+"'>";

then you cann't write:

var x = document.getElementById("1").value;
0

First get all the input elements in the document and filter the input with name you set "myInputs[]" and loop through it and get the input value.

var inputEle = document.getElementsByTagName("input");
    var inputNewlyAdded = [];
    for(var i = 0 ; i < inputEle.length ; i ++){
      if(inputEle[i].name == "myInputs[]"){
         inputNewlyAdded.push(inputEle[i]);
      }
    }

    for(var j = 0 ; j < inputNewlyAdded.length ; j ++){
      console.log(inputNewlyAdded[j].value);
    }
Jagadesh K
  • 237
  • 1
  • 13