Get all selected item elements from all selection items in the form

Hello everyone and thank you for taking the time to answer my question.

I have a form with 6 selections with the "skillLevel" class. I need to get (preferably in an array) the values โ€‹โ€‹of each select element using jQuery.

How can i do this?

+6
source share
6 answers

You can use the map method:

 var arr = $('select.skillLevel').map(function(){ return this.value }).get() 

arr is an array of values.

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

Something like this will do it for you.

 var val = new Array(); $("select.skillLevel").each(function(){ val.push(this.value); }); 
+4
source

Use it like

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

Check out this script

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

Provided by: JqueryApi

+2
source

Hope this fiddle helps you: All selected options

+2
source

I think you should use $(".skillLevel") to select all the elements you want to get, then .val() over them and use .val() to extract the value of each element and process it as you like.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/925563/


All Articles