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?

1
  • 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, 2017 at 2:37

9 Answers 9

550
$('select').on('change', function (e) {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    ....
});
8
  • 10
    What does the $("selector", this) syntax mean? I have a general idea, but I'm not totally sure
    – JoshWillik
    Jan 20, 2014 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, 2014 at 15:07
  • 8
    @JoshWillik: since $() is an alias of jQuery(), you can find the documentation here: 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... May 7, 2014 at 6:54
  • 7
    How to check in case of multiple select? May 25, 2015 at 5:34
  • 4
    @AdrianFöder For you and other people looking for it, .find() is around 10% faster according to this answer: stackoverflow.com/a/9046288/2767703 Jun 8, 2016 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();
   });
2
  • 1
    Binding to option events is a risky proposition, particularly on mobile devices. For example, Webkit doesn't support this event correctly: bugs.chromium.org/p/chromium/issues/detail?id=5284
    – Ian Kemp
    Feb 24, 2016 at 12:23
  • 1
    Ok cool! As I said, the second solution is not supported in many browsers!
    – Bellash
    Feb 29, 2016 at 8: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

1
  • +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 3, 2016 at 6: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.

3
  • 1
    can you please share more detail explanation of your answer ?
    – Mostafiz
    Apr 29, 2016 at 10:39
  • @MostafizurRahman my code very easy that's why i wrote only code.
    – user6270989
    Apr 29, 2016 at 10:43
  • simple and fewer code thanks, I'm looking for this answer. Jan 6, 2021 at 9:31
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;
});
4
5
$('#_SelectID').change(function () {
        var SelectedText = $('option:selected',this).text();
        var SelectedValue = $('option:selected',this).val();
});
0
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, 
});
2
  • if this is the case, why wont "var selectedVal=$("#selectElement").val()" work¿
    – LuisE
    Oct 26, 2017 at 0: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 1, 2018 at 9: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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.