user1789437
user1789437

Reputation: 468

jQuery event if dropdown value =

How can I change this to only execute the function if a dropdown is selected to "No"

$(document).ready(function () {
    $("#dropdown_1").change(function () { 
        $("#textbox_1").val(1);
        $("#textbox_2").val("Classic");
        $("#textbox_3").val(1);
    });
});

Right now, it works no matter when you change it to. I only want the above to happen if dropdown_1 is selected to No

Upvotes: 0

Views: 50

Answers (2)

alex
alex

Reputation: 490453

Wrap it in a condition that checks the dropdown's val().

Alternatively, bail out early. I like this because it can keep indentation down, and you can eliminate many of the do-not-continue cases, allowing the latter of your function's body handle the happy case.

if ($(this).val() != "No") {
    return;
}

Upvotes: 1

Logan Murphy
Logan Murphy

Reputation: 6230

This will do the trick, assuming of course you are dealing with <select id="dropdown_1">

$(function () {
    $("#dropdown_1").change(function () { 
        if($(this).val() == "No") {
            $("#textbox_1").val(1);
            $("#textbox_2").val("Classic");
            $("#textbox_3").val(1);
        }
    });
});

Upvotes: 2

Related Questions