Reputation: 3743
Hi all and thanks for taking the time to answer my question.
I have a form with 6 select elements with the class of "skillLevel". I need to get (preferably in an array] the values of each select element using jQuery.
How can I do that?
Upvotes: 17
Views: 45240
Reputation: 173
You can also use the map
method with an ES6 arrow function like this.
var arr = $('select.skillLevel').get().map(e => e.value)
As in the accepted answer, arr
is an array of values.
Upvotes: 3
Reputation: 3278
Use it as
$(".skillLevel option:selected").each(function () {
alert($(this).text());
});
Check out this Fiddle
Courtesy from : JqueryApi
Upvotes: 4
Reputation: 5110
var array = [];
$('.skillLevel option:selected').each(function() {
array[ $(this).val()] = $(this).text();
});
Upvotes: 16
Reputation: 895
I think that you have to use $(".skillLevel")
to select all the elements you want the value, and then iterate over them, and use .val()
to extract the value of every element, and treat it as you want.
Hope this helps!
Upvotes: 1
Reputation: 187020
Something like this will do that for you.
var val = new Array();
$("select.skillLevel").each(function(){
val.push(this.value);
});
Upvotes: 7
Reputation: 144659
You can use map
method:
var arr = $('select.skillLevel').map(function(){
return this.value
}).get()
arr
is an array of values.
Upvotes: 35