0

I have a set of input tags

<input name= "keys[]" type="text">
<input name= "keys[]" type="text">
<input name= "keys[]" type="text">
<input name= "keys[]" type="text">
<input name= "keys[]" type="text">

Is it possible to get all the input of type text values in an array using the name keys[]

I tried this

$('input[name="keys[]"]').val()

But I got the value of the first input tag only.

I wanted to get a array of the values of these input tags. Is it possible without going thru an iteration?

Thanks

2
  • There is no value attribute? Commented Apr 24, 2014 at 12:24
  • from the manual: Get the current value of the **first element** in the set of matched elements. Commented Apr 24, 2014 at 12:24

4 Answers 4

6

Try serializeArray() it will return an array of objects with name and value.

$('input[name="keys[]"]').serializeArray()
Sign up to request clarification or add additional context in comments.

Comments

1

You can use map:

$('input[name="keys[]"]').map(function(key, input) { return input.value; });

Comments

0

You can try something like

var array= new Array();
$('input[name="keys[]"]').each(function(index){
 array[index] = $(this).val();
});

Hope I help!

Comments

0

http://jsfiddle.net/Gh6Z9/4/

var values = new Array();
$.each( $('input[name="keys[]"]'), function() {
 values.push($(this).val());
});
console.log(values);

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.