Dragan
Dragan

Reputation: 3743

Get all selected options elements from all select elements in a form

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

Answers (7)

audrow
audrow

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

Usman
Usman

Reputation: 3278

Use it as

$(".skillLevel option:selected").each(function () {
            alert($(this).text());
});

Check out this Fiddle

http://jsfiddle.net/zF6HY/1/

Courtesy from : JqueryApi

Upvotes: 4

Ravikumar Sharma
Ravikumar Sharma

Reputation: 3740

Hope this fiddle will help you : All selected options

Upvotes: 2

verisimilitude
verisimilitude

Reputation: 5110

    var array = [];
    $('.skillLevel  option:selected').each(function() {
        array[ $(this).val()] = $(this).text();
    });

Upvotes: 16

Amedio
Amedio

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

rahul
rahul

Reputation: 187020

Something like this will do that for you.

var val = new Array();

$("select.skillLevel").each(function(){
    val.push(this.value);
});

Upvotes: 7

Ram
Ram

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

Related Questions