I have html form with names like ...name="bulk[]".... Is there way to get all values of bulk in array with jquery? Ex
$('[name*="bulk"]').val().join('|');// this is incorrect just to show how I need that
asked Sep 2, 2013 at 16:39
Oleksandr IY
3,1867 gold badges36 silver badges56 bronze badges
-
possible duplicate of How to get an Array with jQuery, multiple <input> with the same nameJohnJohnGa– JohnJohnGa2013年09月02日 16:46:25 +00:00Commented Sep 2, 2013 at 16:46
-
yes seems like duplicate :(Oleksandr IY– Oleksandr IY2013年09月02日 16:52:22 +00:00Commented Sep 2, 2013 at 16:52
6 Answers 6
.map() is made for this purpose
var myarray = $('[name^="bulk"]').map(function(){
return $(this).val()
}).get();
answered Sep 2, 2013 at 16:40
Arun P Johny
389k68 gold badges532 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Oleksandr IY
it says join is not a function
JohnJohnGa
I think what you want is to use join on a native js array, see my answer - you just need the additional call to 'get'
You can use map and get to turn it to a true array!
$('[name*="bulk"]').map(function(){
return $(this).val();
}).get();
answered Sep 2, 2013 at 16:42
JohnJohnGa
15.7k20 gold badges65 silver badges88 bronze badges
Comments
Try the following
var obj = [];
$('[name^="bulk"]').each(function(){
obj.push($(this).val());
});
answered Sep 2, 2013 at 16:41
U.P
7,4527 gold badges43 silver badges63 bronze badges
Comments
You can use .map() to add the names
var myarray = $('[name^="bulk"]').map(function(){
return $(this).val()
}).get();
answered Sep 2, 2013 at 16:41
Rahul Tripathi
173k33 gold badges292 silver badges341 bronze badges
Comments
$elements = $(selector).map(function() { return $(this).val() });
var element_array = $.makeArray($elements);
This will make valid array from jQuery selector.
answered Sep 2, 2013 at 16:43
Flash Thunder
12.1k10 gold badges55 silver badges104 bronze badges
Comments
try this:
var arr = [];
$("[name*="bulk"] :input").each(function(){
var input = $(this);
arr.push(input.val())
});
It will get only input type fields
answered Sep 2, 2013 at 16:44
fujy
5,3015 gold badges33 silver badges51 bronze badges
Comments
lang-js