Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select change event

I am trying to fire an alert depending on which value is selected from a <select> element using jQuery:

$("#optiontype").change( function () {
        var option = $("#optiontype").val();
        if(option.Contains("Forward")){
            alert("forward selected");
        }
    });

The alert never fires whenever I choose 'forward' from the <select> element.

like image 746
brux Avatar asked Jan 29 '26 07:01

brux


2 Answers

Change is the right event. The problem is that contains is not right in this case. Contains is for DOM traversal. What you need to use here is just a normal comparison like so:

$("#optiontype").change( function () {
    var option = $("#optiontype").val();
    if(option.toLowerCase() == "forward"){
        alert("forward");
    }
});

Or if the string can contain more than just "forward", check out indexOf: http://www.w3schools.com/jsref/jsref_indexof.asp

like image 78
jValdron Avatar answered Jan 30 '26 19:01

jValdron


You should use option.indexOf("forward") != -1

like image 29
parapura rajkumar Avatar answered Jan 30 '26 20:01

parapura rajkumar