2

I have an input field that has a button to dynamically add another input field and I am trying to get it so that when I click plot i am able to grab the content inside the input fields gps[]

html

<div id="marker">
 <input type="text" name="gps[]" id="gps">
 <input type="text" name="gps[]">
</div>

Plot

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 = "<input type='text' name='gps[]'>";
 document.getElementById(divName).appendChild(newdiv);
 counter++;
 }
}
$('body').on('click','.plot', function(){
 var y = $('input[name="gps[]"]').val();
 console.log(y);
});
asked Jan 12, 2014 at 17:18

1 Answer 1

3

You have to use .map to get the value of all the elements in the collection :

var y = $('input[name="gps[]"]').map(function() {return this.value;}).get();

FIDDLE

answered Jan 12, 2014 at 17:24
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.