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.
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
You should use option.indexOf("forward") != -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With