Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all "option" values in "select" by Jquery?

Tags:

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

like image 585
Jin Yong Avatar asked Oct 13 '09 23:10

Jin Yong


3 Answers

var values = $("#group_select>option").map(function() { return $(this).val(); });
like image 163
eulerfx Avatar answered Nov 15 '22 13:11

eulerfx


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.

like image 21
Russ Cam Avatar answered Nov 15 '22 15:11

Russ Cam


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()
};
like image 4
helloandre Avatar answered Nov 15 '22 13:11

helloandre