user1040259
user1040259

Reputation: 6509

JQuery - run function if checkbox is checked

If checked: #instructions-bar will activate. If not checked: #instructions-bar will not activate.

What's the best way to accomplish this within a case statement.. I know multiple if then statements wouldn't be the best way. Basically, I want to hide the #instructions-bar within each case. Not the whole switch.

<p><input type="checkbox" id="unique-checkbox-name" name="enable-checkbox"/>Checkbox</p>

 $("select[name=map-tool]").change(function (e) {
    var currVal = $(this).val();
    switch (currVal) {
        case "navigation":
            // instructions bar HIDE
            $('#instructions-bar').slideUp('slow');
            // more code
            break;
        case "distance":
            // instructions bar SHOW
            $('#instructions-bar').slideDown('slow');
           // more code
            break;
        case "area":
            // instructions bar HIDE
            $('#instructions-bar').slideUp('slow');
            // more code
            break;
    }
});

Upvotes: 2

Views: 3106

Answers (1)

Jake Feasel
Jake Feasel

Reputation: 16955

 $("select[name=map-tool]").change(function (e) {
    var currVal = $(this).val();
    var isChecked = $("#unique-checkbox-name").is(":checked");
    switch (currVal) {
        case "navigation":
            // instructions bar HIDE
            if (isChecked) $('#instructions-bar').slideUp('slow');
            // more code
            break;
        case "distance":
            // instructions bar SHOW
            if (isChecked) $('#instructions-bar').slideDown('slow');
           // more code
            break;
        case "area":
            // instructions bar HIDE
            if (isChecked) $('#instructions-bar').slideUp('slow');
            // more code
            break;
    }
});

Upvotes: 2

Related Questions