4

I have x number of input fields with class='agency_field'. How can I create a JS array that contain the values of all fields with this class?

Using jQuery, this gives a syntax error:

$(".agency_field").each(function(index) { agencies[] = $(this).val(); });

asked Apr 5, 2011 at 13:15
22
  • Holy @#%# - you just got 6 answers within a minute of each other. Commented Apr 5, 2011 at 13:19
  • Hard to beat SO response times! Commented Apr 5, 2011 at 13:20
  • LOL Five each() answers, and only one map() answer... Commented Apr 5, 2011 at 13:23
  • @Šime - I'm not sure how well-known map() is. each() is seen all over the place and it's easy to use (more familiarity). I gave this karim's answer +1 though because the documentation says map() is especially useful for this scenario. Commented Apr 5, 2011 at 13:27
  • That being said, where's the love for the each() answers? They're still acceptable answers, and more readable IMHO. Commented Apr 5, 2011 at 13:30

7 Answers 7

10

You can use .map instead, which is perhaps more suited to your purpose:

var values = $(".agency_field").map(function() {
 return this.value;
}).get();
alert(values.join(","));
answered Apr 5, 2011 at 13:18
Sign up to request clarification or add additional context in comments.

11 Comments

@karim79: Never seen this before... Interesting approach. How does this compare speed wise to each/push? Time for some api reading methinks..
@karim No need for .join(","). When arrays coerce to strings, the commas are added by default.
@James South - I can't imagine there would be any perceptible difference in performance for a typical application. I use it because it was literally designed for sucking properties out of collections in a concise manner, as the manual says: "The .map() method is particularly useful for getting or setting the value of a collection of elements."
@Šime Vidas - I know, I put it there to highlight that what we're getting is an array.
@karim: Qood to know. Cheers!
|
0
var agencies = [];
$(".agency_field").each(function(index) { agencies.push($(this).val()); });
answered Apr 5, 2011 at 13:17

1 Comment

@stef, you shouldn't choose the first answer. You should choose the best answer - which in this case is karim's by vote count.
0

You're creating a new array for each iteration. Try instead instantiating an array before the each call and adding to the array each iteration.

answered Apr 5, 2011 at 13:18

Comments

0
var arr = []; $(".agency_field").each(function(index) { arr.push($(this).val()); });

arr would contain what you want in the end.

answered Apr 5, 2011 at 13:18

Comments

0

Your code shd be slightly changed to:

var agencies = [];
$(".agency_field").each(function(index) { 
 agencies.push($(this).val()); 
});
answered Apr 5, 2011 at 13:18

Comments

0

You need to create an array initially and then add each value to that array:

var agencies = [];
$(".agency_field").each(function(index) { agencies.push($(this).val()) });
answered Apr 5, 2011 at 13:18

Comments

0

You're quite used to a language like php? ;) In Javascript, you'd use array.push() for appending to an array; so

$(".agency_field").each(function(index) { agencies.push( $(this).val() ); });
answered Apr 5, 2011 at 13:20

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.