1

Can someone tell me if this jquery code makes sense. I have it wrapped in a document ready block. I am trying to hide an element if the set of conditions are true, but when I click on the submit button, knowing that one of the checkboxes is indeed checked, the element appears instead of being hidden. Is there anything obvious that I am missing with the below code:

            $('#submit').on('click', function() {
            if ($('#Option1').not(':checked') &&
            $('#Option2').not(':checked') &&
            $('#Option3').not(':checked') &&
            $('#Option4').not(':checked') &&
            $('#Option5').not(':checked')) {
                $("form").submit(function () {
                    return false;
                });
                $('#selectOption').show();
            } else {
                $('#selectOption').hide();
            }
        });
0

1 Answer 1

2

There really is no use in hiding or showing anything when the form is submitted, as it will redirect the page anyway. I'd do something like this and check to see if any boxes with an ID starting with Option is checked, and then prevent the click if no boxes are checked etc:

$('#submit').on('click', function(e) {
    if (!$('[id^="Option"]').is(':checked')) {
         e.preventDefault();
         $('#selectOption').show();
    }
});
Sign up to request clarification or add additional context in comments.

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.