278

I bound an event on the change event of my select elements with this:

$('select').on('change', '', function (e) {

});

How can I access the element which got selected when the change event occurs?

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • 6
    @superuberduper I avoided jquery for as long as I could but resistance is futile. You'll feel so much better after you've been assimilated. – adg Apr 27 '17 at 02:37

9 Answers9

550
$('select').on('change', function (e) {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    ....
});
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 10
    What does the $("selector", this) syntax mean? I have a general idea, but I'm not totally sure – JoshWillik Jan 20 '14 at 22:08
  • 16
    @JoshWillik with `$("selector", this)` you are finding all `selector` elements inside `this`'s context. Writing `$("selector", this)` is almost the same than `$(this).find('selector')` – Bellash Jan 24 '14 at 15:07
  • 1
    Is there a documentation for this somewhere? I looked around the jQuery docs and didn't find anything. – JoshWillik Jan 24 '14 at 15:14
  • 8
    @JoshWillik: since ``$()`` is an alias of ``jQuery()``, you can find the documentation here: https://api.jquery.com/jQuery/ The signature stated there is obviously ``jQuery( selector [, context ] )``. @Bellash: if it's "almost the same", what is the difference? Or what is faster? I prefer ``.find()`` since this is more OO IMO... – Adrian Föder May 07 '14 at 06:54
  • 7
    How to check in case of multiple select? – Hemant_Negi May 25 '15 at 05:34
  • @Neal get the changed option for a multi select. – Hemant_Negi May 27 '15 at 04:15
  • @Hemant_Negi Hmm I do not know off-hand, that might make a good stack overflow question if it has not been asked already :-) Let me know if you find the answer :-D – Naftali May 27 '15 at 12:53
  • 4
    @AdrianFöder For you and other people looking for it, `.find()` is around 10% faster according to this answer: http://stackoverflow.com/a/9046288/2767703 – Kevin van Mierlo Jun 08 '16 at 12:39
92

You can use the jQuery find method

 $('select').change(function () {
     var optionSelected = $(this).find("option:selected");
     var valueSelected  = optionSelected.val();
     var textSelected   = optionSelected.text();
 });

The above solution works perfectly but I choose to add the following code for them willing to get the clicked option. It allows you get the selected option even when this select value has not changed. (Tested with Mozilla only)

    $('select').find('option').click(function () {
     var optionSelected = $(this);
     var valueSelected  = optionSelected.val();
     var textSelected   = optionSelected.text();
   });
Bellash
  • 7,560
  • 6
  • 53
  • 86
  • 1
    Binding to `option` events is a risky proposition, particularly on mobile devices. For example, Webkit doesn't support this event correctly: https://bugs.chromium.org/p/chromium/issues/detail?id=5284 – Ian Kemp Feb 24 '16 at 12:23
  • 1
    Ok cool! As I said, the second solution is not supported in many browsers! – Bellash Feb 29 '16 at 08:10
20

Delegated Alternative

In case anyone is using the delegated approach for their listener, use e.target (it will refer to the select element).

$('#myform').on('change', 'select', function (e) {
    var val = $(e.target).val();
    var text = $(e.target).find("option:selected").text(); //only time the find is required
    var name = $(e.target).attr('name');
}

JSFiddle Demo

Community
  • 1
  • 1
DanielST
  • 13,783
  • 7
  • 42
  • 65
  • +1 because this is what i want, but when i tried this locally, its not working .Only work at jsfiddle, what are the CDN do i have to add? – AVI Apr 03 '16 at 06:48
12
<select id="selectId">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>


$('#selectId').on('change', function () {
     var selectVal = $("#selectId option:selected").val();
});

First create a select option. After that using jquery you can get current selected value when user change select option value.

7

I find this shorter and cleaner. Besides, you can iterate through selected items if there are more than one;

$('select').on('change', function () {
     var selectedValue = this.selectedOptions[0].value;
     var selectedText  = this.selectedOptions[0].text;
});
user229044
  • 232,980
  • 40
  • 330
  • 338
tozlu
  • 4,667
  • 3
  • 30
  • 44
  • `selectedOptions` is not available in all browsers. – Bernhard Döbler Dec 13 '16 at 10:13
  • @BernhardDöbler which ones? any source? – tozlu Dec 13 '16 at 11:37
  • 1
    I copied your code to IE 11 and got an error. I ended up with `this.options[ this.options.selectedIndex ]`. [Mozilla](https://developer.mozilla.org/de/docs/Web/API/HTMLSelectElement) says is supported by Firefox from 26, IE No support. – Bernhard Döbler Dec 14 '16 at 08:36
  • It is supported by all browsers except IE. https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions – tozlu Oct 02 '20 at 14:43
5
$('#_SelectID').change(function () {
        var SelectedText = $('option:selected',this).text();
        var SelectedValue = $('option:selected',this).val();
});
Serdar Karaca
  • 115
  • 1
  • 7
3

Another and short way to get value of selected value,

$('#selectElement').on('change',function(){
   var selectedVal = $(this).val();
                       ^^^^  ^^^^ -> presents #selectElement selected value 
                         |
                         v
               presents #selectElement, 
});
Recep Can
  • 121
  • 1
  • 9
  • if this is the case, why wont "var selectedVal=$("#selectElement").val()" work¿ – LuisE Oct 26 '17 at 00:39
  • The case is getting value when change event emitted on select element. Value wouldn't be updated when select changes if you do like that. – Recep Can Oct 01 '18 at 09:27
3

This can also work fine

(function(jQuery) {
  $(document).ready(function() {
    $("#select_menu").change(function() {
      var selectedOption = $("#select_menu").val()
    });
  });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
2

See official API documentation https://api.jquery.com/selected-selector/

This good works:

$( "select" ).on('change',function() {
  var str = "";
  // For multiple choice
  $( "select option:selected" ).each(function() {
    str += $( this ).val() + " "; 
  });
});

and

$( "select" ).on('change',function() {
  // For unique choice
  var selVal = $( "select option:selected" ).val(); 
});

and be easy for unique choice

var SelVal = $( "#idSelect option:selected" ).val();
alex Roosso
  • 121
  • 4