Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JS array from input field values

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(); });

like image 714
stef Avatar asked Dec 27 '22 22:12

stef


1 Answers

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(","));
like image 186
karim79 Avatar answered Jan 04 '23 23:01

karim79