n00b
n00b

Reputation: 2768

jQuery get selected option value (not the text, but the attribute 'value')

Okay, I have this code:

<select name="selector" id="selector">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

And I'd like to get the value of the option selected. Example: 'Option 2' is selected, and the value of it is '2'. The '2' is the value I need to get and not 'Option 2'.

Upvotes: 188

Views: 651213

Answers (15)

Hadeer Al-Masri
Hadeer Al-Masri

Reputation: 21

$('#myOptions').change(function() {
  var value = $("#myOptions option:selected").val();
  alert(value);
});
<select name="selector" id="myOptions">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</select>

Upvotes: 2

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79850

04/2020: Corrected old answer

Use :selected pseudo selector on the selected options and then use the .val function to get the value of the option.

$('select[name=selector] option').filter(':selected').val()

Side note: Using filter is better then using :selected selector directly in the first query.

If inside a change handler, you could use simply this.value to get the selected option value. See demo for more options.

//ways to retrieve selected option and text outside handler
console.log('Selected option value ' + $('select option').filter(':selected').val()); 
console.log('Selected option value ' + $('select option').filter(':selected').text());

$('select').on('change', function () {
  //ways to retrieve selected option and text outside handler
  console.log('Changed option value ' + this.value);
  console.log('Changed option text ' + $(this).find('option').filter(':selected').text());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select>
  <option value="1" selected>1 - Text</option>
  <option value="2">2 - Text</option>
  <option value="3">3 - Text</option>
  <option value="4">4 - Text</option>
</select>

Old answer:

Edit: As many pointed out, this does not returns the selected option value.

~~Use .val to get the value of the selected option. See below,

$('select[name=selector]').val()~~

Upvotes: 317

Rashiqul Rony
Rashiqul Rony

Reputation: 89

It's working better. Try it.

let value = $("select#yourId option").filter(":selected").val();

Upvotes: 1

Code Spy
Code Spy

Reputation: 9964

Source Link

Use jQuery val() to GET Selected Value and and text() to GET Option Text.

    <select id="myDropDown" class="form-control">
        <option value="0">Select Value 0</option>
        <option value="8">Option value 8</option>
        <option value="5">Option value 5</option>
        <option value="4">Option value 4</option>
    </select>

Change Event on Select Dropdown

        $("#myDropDown").change(function () {

            // Fetching Value
            console.log($(this).val());

            // Fetching Text
            console.log($(this).find('option:selected').text());

            alert('Value: '+$(this).val()+' | Text: '+$(this).find('option:selected').text());
        });

Button Click

        $("button").click(function () {

        // Fetching Value
            console.log($("#myDropDown").val());

    // Fetching Text
            console.log($('#myDropDown option:selected').text());

            alert('Value: '+$("#myDropDown").val()+' | Text: '+$('#myDropDown option:selected').text());
        });

Upvotes: 1

David Torres
David Torres

Reputation: 1767

I just wanted to share my experience

For me,

$('#selectorId').val()

returned null.

I had to use

$('#selectorId option:selected').val()

Upvotes: 138

Turan Zamanlı
Turan Zamanlı

Reputation: 3926

 $(document ).ready(function() {
    $('select[name=selectorname]').change(function() { 
     alert($(this).val());});
 });

Upvotes: 2

Abolfazl Miadian
Abolfazl Miadian

Reputation: 3159

this code work very well for me: this return the value of selected option. $('#select_id').find('option:selected').val();

Upvotes: 1

Saddam Khan
Saddam Khan

Reputation: 21

$('#selectorID').val();

OR

$('select[name=selector]').val();

OR

$('.class_nam').val();

Upvotes: 0

Payal Mittal
Payal Mittal

Reputation: 121

Try this:

$(document).ready(function(){
    var data= $('select').find('option:selected').val();
});

or

var data= $('select').find('option:selected').text();

Upvotes: 8

user2962280
user2962280

Reputation:

Look at the example:

    <select class="element select small" id="account_name" name="account_name"> 

        <option value="" selected="selected">Please Select</option>           
        <option value="paypal" >Paypal</option>
        <option value="webmoney" >Webmoney</option>

    </select>

 <script type="text/javascript">

        $(document).ready(function(){ 
             $('#account_name').change(function(){
               alert($(this).val());                                                          
             });
        });
 </script>

Upvotes: 0

Gavin
Gavin

Reputation: 7976

One of my options didn't have a value: <option>-----</option>. I was trying to use if (!$(this).val()) { .. } but I was getting strange results. Turns out if you don't have a value attribute on your <option> element, it returns the text inside of it instead of an empty string. If you don't want val() to return anything for your option, make sure to add value="".

Upvotes: 2

Geziel Carvalho
Geziel Carvalho

Reputation: 301

For a select like this

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option id="0">CHOOSE AN OPTION</option>
   <option id="127">John Doe</option>
   <option id="129" selected>Jane Doe</option>

... you can get the id this way:

$('#list-name option:selected').attr('id');

Or you can use value instead, and get it the easy way...

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option value="0">CHOOSE AN OPTION</option>
   <option value="127">John Doe</option>
   <option value="129" selected>Jane Doe</option>

like this:

$('#list-name').val();

Upvotes: 3

Marcus
Marcus

Reputation: 5457

You need to add an id to your select. Then:
$('#selectorID').val()

Upvotes: 10

Usman
Usman

Reputation: 3278

you can use jquery as follows

SCRIPT

  $('#IDOfyourdropdown').change(function(){
    alert($(this).val());
  });

FIDDLE is here

Upvotes: 6

j08691
j08691

Reputation: 208031

$('select').change(function() {
    console.log($(this).val())
});​

jsFiddle example

.val() will get the value.

Upvotes: 25

Related Questions