0

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

1

2 Answers 2

2

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);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.