Danish Kundra
Danish Kundra

Reputation: 124

How to get values of input types by name

This is how i have written my values.

<input type='text' class='textVerdana11' style='text-align:right' id='test1' name='test[]' value='1' />
<input type='text' class='textVerdana11' style='text-align:right' id='test2' name='test[]' value='2' />
<input type='text' class='textVerdana11' style='text-align:right' id='test3' name='test[]' value='3' />

Now the problem at hand is that i have to extract the values by name because the ids can be anything in jQuery or JavaScript.

Upvotes: 1

Views: 70

Answers (3)

Satpal
Satpal

Reputation: 133453

Attribute Equals Selector [name="value"] can be used to select elements, then map() to create an array of values.

 var array = $('[name="test[]"]').map(function() {
   return $(this).val();
 }).get();

Upvotes: 4

mic4ael
mic4ael

Reputation: 8320

I think, something like this should work

$('[name="test[]"]').each(function() {
    return $(this).val();
}).get()

Upvotes: 1

Hiren Master
Hiren Master

Reputation: 105

you can use like this :

$(function(){
var values = $("input[name='test\\[\\]']")
              .map(function(){return $(this).val();}).get();

alert(values);
});

Upvotes: 1

Related Questions