Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to see if a drop down menu select has changed [duplicate]

Tags:

jquery

I would like to see if a particular drop down menu item has been selected or deselected.

I have tried

jQuery("#dropdownID option[value='selectionKey']").change(function() {
    if (jQuery("#dropdownID option[value='selectionKey']").attr('selected', 'selected'))
        DoSomething();
    else DoSomethingElse();
});

and

jQuery("#dropdownID").change(function() {
    if (jQuery("#dropdownID option[value='selectionKey']").attr('selected', 'selected'))
        DoSomething();
    else DoSomethingElse();
});

but neither block is triggered by changing the selection in the drop down menu. That is, it never gets to the if statement.

like image 481
OtagoHarbour Avatar asked Jul 05 '13 19:07

OtagoHarbour


2 Answers

You can try something like this in jquery versions less than 1.7:

$("#dropdownID").live('change', function() {
    if ($(this).val() == 'selectionKey'){
        DoSomething();
    } else {
        DoSomethingElse();
    }
});

You can try something like this in jquery versions greater than 1.7:

$("#dropdownID").on('change', function() {
    if ($(this).val() == 'selectionKey'){
        DoSomething();
    } else {
        DoSomethingElse();
    }
});
like image 189
tnschmidt Avatar answered Oct 12 '22 12:10

tnschmidt


Try these:

jQuery("#dropdownID").change(function() {
    if (jQuery("#dropdownID option[value=selectionKey]").attr('selected', 'selected')) {
        DoSomething();
    }
    else {
DoSomethingElse();
}
});
like image 26
Lightsaber Avatar answered Oct 12 '22 11:10

Lightsaber