Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery event when user makes selection in dropdown

Tags:

jquery

I have a drop down list. In Jquery what is the event I would use when the user makes a selection.

The id of the dropdown is drp1

I tried the following but did not work:

$("#ddrp1").SelectChanged(SelectionItem);
like image 747
Nate Pet Avatar asked Apr 27 '12 15:04

Nate Pet


1 Answers

Use the change() event:

$("#ddrp1").change(function() {
    // Pure JS
    var selectedVal = this.value;
    var selectedText = this.options[this.selectedIndex].text;

    // jQuery
    var selectedVal = $(this).find(':selected').val();
    var selectedText = $(this).find(':selected').text();
});

In jQuery 1.7, you can use .on()

$("#ddrp1").on("change", function() {
    // Pure JS
    var selectedVal = this.value;
    var selectedText = this.options[this.selectedIndex].text;

    // jQuery
    var selectedVal = $(this).find(':selected').val();
    var selectedText = $(this).find(':selected').text();
}​​​​);​

Here's a working jsFiddle using on()

like image 185
James Hill Avatar answered Sep 20 '22 04:09

James Hill