4

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?

alex
  • 479,566
  • 201
  • 878
  • 984
gurehbgui
  • 14,236
  • 32
  • 106
  • 178

5 Answers5

18

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.

alex
  • 479,566
  • 201
  • 878
  • 984
1
$('#selectmenu1').on('change', function(){
     alert( this.value );
});
Engineer
  • 47,849
  • 12
  • 88
  • 91
1

Here is the right answer for Jquery Mobile device .

$(document).bind( "change",'#selectmenu1', function(event, ui) {
    alert($( '#selectmenu1' ).val());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
 
    
    
<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>
 
Pavnish Yadav
  • 2,728
  • 3
  • 15
  • 14
-1

http://jsfiddle.net/ipsjolly/5RaDB/

<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>

<div id="getval" ></div>


<script>
 function displayVals() {
       var singleValues = $("select").val();
      $("#getval").html(singleValues);
  }
  $('#selectmenu1').change(function() {
      displayVals();
  });​
</script>
Code Spy
  • 9,626
  • 4
  • 66
  • 46
-1

try this.. this is the simplest way..

  <select name="selectmenu1" id="selectmenu1" onchange="selectvalue(this.value)>
                        <option value="o1">
                           a
                        </option>
                        <option value="o2">
                            b
                        </option>
                        <option value="o3">
                            c
                        </option>
                        <option value="o4">
                            d
                        </option>
                    </select>

On JavaScript side code is ..

function selectvalue(value){

alert(value);

}
Owais Iqbal
  • 571
  • 3
  • 12