Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get values of all textboxes with same name attributes in jquery

I need to get values of all textboxes with same name attributes using jquery.

<input type="text" id="text1" name="text[]">
<input type="text" id="text2" name="text[]">
<input type="text" id="text3" name="text[]">

How can I get all values of textbox text[] and compare it using jquery.

I tried using

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

but am no successful.

like image 230
Developer Avatar asked Dec 17 '12 15:12

Developer


4 Answers

You can use map method and store the values into an array.

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

http://jsfiddle.net/UugWW/

like image 155
undefined Avatar answered Nov 13 '22 08:11

undefined


This one should work :

$('input[name="text[]"]');

You can loop on it to get all values.

$('input[name="text[]"]').each(function() {
    alert($(this).val());
});
like image 45
Magus Avatar answered Nov 13 '22 09:11

Magus


Let's split the requirement into smaller problems.

First you want to select all those inputs.

var $inputs = $("input[name='text[]']")

It returns a jQuery object, containing all the input named text[]. You also might not need to use square brackets into the name.

var inputs = $inputs.get();

Extract the matching elements into a plain Array, so that we can now access Array's prototype methods, such as Array.prototype.map.

var values = inputs.map(function takeValue(input) {
    return input.value;
});
like image 2
Bruno Avatar answered Nov 13 '22 09:11

Bruno


Use a selector like this:

$('input[type="text"][name="text[]"')
like image 1
dsgriffin Avatar answered Nov 13 '22 09:11

dsgriffin