How to get all “option” values in “select” by Jquery?

Does anyone know how can I get all values in the select by Jquery?

Example:

<select name="group_select" id="group_select">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
</select>

Which I want to get all option values (a,b and c) from select (without selected any option) by using jquery

13.10.2009 23:11:11
PhoneixS 9.07.2013 10:24:10
4 ОТВЕТА
РЕШЕНИЕ
var values = $("#group_select>option").map(function() { return $(this).val(); });
45
13.10.2009 23:13:19
I had to add a .get() to the end... var values = $("#group_select>option").map(function() { return $(this).val(); }).get();
plong0 12.02.2015 08:01:26
@plong0 yes we have to use val() to get it in Array
PriyankMotivaras 14.02.2020 11:41:13

The following will give you an array of the <option> values

var values = $.map($('#group_select option'), function(e) { return e.value; });

// as a comma separated string
values.join(',');

Here's a Working Demo. add /edit to the URL to see the code.

20
13.10.2009 23:13:41
Thanks for the values.join(','); , this answer was more applicable to my solution.
HPWD 8.10.2012 14:58:54
Single line: var $('#group_select option').map(function() { return this.value; }).get().join(); [link(api.jquery.com/map)
JoePC 26.07.2018 21:39:30

This will iterate through all the options and give you their values.Then you can either append them to an array or manipulate them individually.

$('#group_select option').each(function(){
    $(this).val()
};
3
13.04.2016 14:49:10
I think closing bracket of each loop missing.
Connectify_user 13.03.2020 06:08:10

You can use following code for that :-

var assignedRoleId = new Array();
$('#RolesListAssigned option').each(function(){
        assignedRoleId.push(this.value);
        assignedRoleId.push(this.text);
});
2
19.02.2014 12:49:38