Alert on value change in select

21,366

something like this perhaps: http://jsfiddle.net/mEFQq/1/

You need to use the .val() function, not the .text() function when working with selects.

Also you need to compare with the value attribute (1 in this case) not the actual text value.

The code looks like this:

$(document).ready(function() {
    $("select").change(function(){
        if($(this).val() == "1")
            {
                alert('value 1 (wich refers to Chef) got selected');
            }
        });
});
Share:
21,366
TLR
Author by

TLR

Updated on April 10, 2020

Comments

  • TLR
    TLR about 4 years

    I'm trying to alert the user when the value of a select changes to a specific one, but I have multiple select in my page (one for each user).

    Here is what I tried: http://jsfiddle.net/mEFQq/

    $(document).ready(function() {
        $("select").change(function(){
            if($(this).text() == "Chef")
                {
                    alert();
                }
            });
    });
    

    My wish is to have an alert when the select changes to the value "Chef".

    How do I do this?