187

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

Peter O.
  • 32,158
  • 14
  • 82
  • 96
n00b
  • 2,738
  • 2
  • 20
  • 31

15 Answers15

315

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()~~
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • 7
    Do ur test: `$('select[name=selector]').change(function() { alert($(this).val()); });` – KingRider Oct 08 '15 at 14:06
  • 4
    Note that if the selected option has no `value` attribute specified, the label (i.e. ``) will be returned. Might not always be what you want. Of course, in most applications, value is specified. – Czechnology Apr 11 '17 at 21:04
  • 1
    Why is using `filter` better? Also why does `.val()` not work? It seems to work at least in Chrome. It's not clear from the comments. Thanks for your help! – Crashalot Jun 15 '21 at 08:40
  • for me .val() seems to work fine, any reason not to use it? – garek007 Jul 29 '21 at 21:15
  • This doesn't work, but if you use `.find` instead of `.filter` you might have success :wink: – b264 Oct 01 '21 at 05:19
  • Why is this? -> Side note: Using filter is better then using :selected selector directly in the first query. – David Torres Sep 07 '22 at 08:17
138

I just wanted to share my experience

For me,

$('#selectorId').val()

returned null.

I had to use

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
David Torres
  • 1,767
  • 1
  • 16
  • 19
  • 1
    Was that browser specific case? Did the newest version of chrome not allow it, or was it IE that didnt support it? – Theodor Solbjørg Mar 10 '15 at 17:50
  • 1
    It was in the newest version of Chrome. Maybe also in Firefox, but not sure. – David Torres Mar 11 '15 at 17:55
  • Not sure why `$('#selectorId option:selected').val()` didn't worked for me. Instead I used `$('select option:selected').val()` – Francisco Quintero Jun 09 '15 at 02:50
  • 1
    @Francisco The problem is that `$('select option:selected').val()` will take the value of the first selector in the HTML code. Whereas with `$('#selectorId option:selected').val()` you can have the value of the selector which id you specified. Double check for typos. Here is an example that shows what I just said: http://codepen.io/anon/pen/Nqgqyz – David Torres Jun 10 '15 at 08:25
25
$('select').change(function() {
    console.log($(this).val())
});​

jsFiddle example

.val() will get the value.

j08691
  • 204,283
  • 31
  • 260
  • 272
10

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

Marcus
  • 5,407
  • 3
  • 31
  • 54
8

Try this:

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

or

var data= $('select').find('option:selected').text();
andreas
  • 16,357
  • 12
  • 72
  • 76
Payal Mittal
  • 121
  • 2
  • 2
6

you can use jquery as follows

SCRIPT

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

FIDDLE is here

Usman
  • 3,200
  • 3
  • 28
  • 47
3

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();
Geziel Carvalho
  • 191
  • 1
  • 3
  • 12
2

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="".

Gavin
  • 7,544
  • 4
  • 52
  • 72
2
 $(document ).ready(function() {
    $('select[name=selectorname]').change(function() { 
     alert($(this).val());});
 });
Turan Zamanlı
  • 3,828
  • 1
  • 15
  • 23
  • While this code could solve the problem, it is best to add elaboration and explain how it works for people who might not understand this piece of code. – Papershine May 02 '18 at 11:34
  • 1
    @paper1111 this is simple functions. I can explain if this is needed. $('select[name = here is select box name]) .change() - function its mean that when we change select box option function will be work. $(this).val() - it return selected option value – Turan Zamanlı May 03 '18 at 10:20
2

$('#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>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jul 20 '22 at 11:40
1

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

Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15
1

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());
        });
Code Spy
  • 9,626
  • 4
  • 66
  • 46
0

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>
JH_
  • 406
  • 1
  • 4
  • 15
0
$('#selectorID').val();

OR

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

OR

$('.class_nam').val();
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
  • 1
    @FarhadBagherlo "Keep typing" is **not** a useful edit comment! Please summarize your changes, so that readers of the edit history understand what you've done without inspecting every changed character. Thanks! – jpaugh Aug 22 '17 at 21:12
0

It's working better. Try it.

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

Rashiqul Rony
  • 79
  • 1
  • 7