Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected item from select menu in jQuery/jQuery Mobile?

I have a jQuery Mobile code like this:

<select name="selectmenu1" id="selectmenu1">
                        <option value="o1">
                           a
                        </option>
                        <option value="o2">
                            b
                        </option>
                        <option value="o3">
                            c
                        </option>
                        <option value="o4">
                            d
                        </option>
                    </select>

Now I want to get the selected value after the user changed the selection.

How to do this with jQuery?

like image 951
gurehbgui Avatar asked Dec 02 '22 22:12

gurehbgui


2 Answers

It's very simple...

$("#selectmenu1").change(function() {
    var selected = $(this).val(); // or this.value
});

jsFiddle.

If you wanted the selected element's text node(s), use this instead...

$("#selectmenu1").change(function() {
    var selected = $(this).find(":selected").text();
});​

jsFiddle.

like image 168
alex Avatar answered Dec 18 '22 22:12

alex


$('#selectmenu1').on('change', function(){
     alert( this.value );
});
like image 21
Engineer Avatar answered Dec 19 '22 00:12

Engineer