user1206480
user1206480

Reputation: 1858

Hiding element with jquery

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

Upvotes: 1

Views: 566

Answers (1)

adeneo
adeneo

Reputation: 318182

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

Upvotes: 2

Related Questions