1

I am trying to get option value of an already selected option in the select choice list. How can I make it possible. Here is my code snippet

$(document).ready(function(){
    $("#getCusineType").bind('click', function() {
         alert($(this).find('option:selected').val());
    });
    $("#getCusineType").bind('focusout', function() {
         alert($(this).find('option:selected').val());
    });
    function getCusineType(){
      var getCusineType = $('#getCusineType').val();
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="getCusineType" onchange="getCusineType()">
   <option value="1">Indian</option>
   <option value="2">American</option>
   <option value="3">Italian</option>
   <option value="4">General</option>                 
 </select>
Ram Segev
  • 2,563
  • 2
  • 12
  • 24

2 Answers2

0

If you want to get selected option value:

var value=$("#getCusineType").val();

or text

var text=$("#getCusineType option:selected").text();

If you want to get the selected value after change event, you should bind a change event handler.

$("#getCusineType").change(function() {
   alert($(this).val());
})

console.log("Value is: "+$('#getCusineType').val());
console.log("Text is: "+$("#getCusineType option:selected").text());
$("select").click(function() {
       console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="getCusineType">
         <option value="1">Indian</option>
         <option value="2">American</option>
         <option value="3">Italian</option>
         <option value="4">General</option>                 
</select>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

simply way to trigger the on change event

 $(function(){
      $("#getCusineType").on('change', function(){
        alert($(this).val());
      }).trigger('change');
    });
jvk
  • 2,133
  • 3
  • 19
  • 28