3

This is my code: https://jsfiddle.net/56xh3rLo/

Here is the javascript from the jsfiddle:

$(document).ready(function() {
    $(document).click(function(event) { //line 1
        if(!$(event.target).closest('.menu').length) { //line 2
            if ($('.menu-btn').is(':checked')) { //line 3
                $('.menu-btn').trigger('click'); //line 4
            }
        }        
    })
})

as you can see, line 1 is to detect if a user is going to click on the page. line 2 is to detect if the user clicks on anywhere EXCEPT for the .menu element (which is anywhere in the black background). Line 3 is to detect if the .menu-btn checkbox is checked. if it is checked, it will trigger a click on the checkbox.

So, here is the summary of the code: if the user clicks on anywhere EXCEPT for the black box area and the checkbox IS checked, the code will trigger a click on the checkbox and it will uncheck the checkbox. the problem is, the checkbox does not even check at all. why is it not working?

5
  • jsfiddle.net/56xh3rLo/1 Commented Oct 14, 2016 at 18:13
  • 1
    Your handler is setup to trigger a click on the checkbox every time it's checked...so you're immediately unchecking it. Commented Oct 14, 2016 at 18:15
  • @Rayon now i can check the checkbox BUT i cannot uncheck the checkbox when i click somewhere outside of the black box area. Commented Oct 14, 2016 at 18:15
  • @AndréDion i don't understand, how do i fix it? Commented Oct 14, 2016 at 18:16
  • jsfiddle.net/56xh3rLo/5 Commented Oct 14, 2016 at 18:32

3 Answers 3

5

You want to change your outer if check to

if(!$(event.target).closest('.menu').length && !$(event.target).hasClass('menu-btn')) {

The way you're doing it now is seeing if the click target has an ancestor with the .menu class, but if you click the checkbox, it will not have that ancestor. This means the outer if check will pass, then the inner if check will pass because the click you just did checked the box. That will run the line

$('.menu-btn').prop( "checked", false );

Here's the working example.

Sign up to request clarification or add additional context in comments.

6 Comments

I do believe this is the answer the OP is looking for
Yes, thanks it worked but look here: jsfiddle.net/56xh3rLo/7 I want to click on the label and check the checkbox. that works. but i cannot uncheck the checkbox is the label. how do i fix this?
You want to click the label to check or uncheck the checkbox?
exactly. I want to click the label to check and uncheck the checkbox. I ALSO want to uncheck the checkbox if the checkbox is checked and if the user clicks anywhere EXCEPT for black area box.
Yep it looks like you got it, nice job.
|
2

The proper way to uncheck a checkbox with jQuery:

$('.menu-btn').prop( "checked", false );

2 Comments

oh i didn't know that but it still doesn't work: jsfiddle.net/56xh3rLo/2
@pixie123 your jsfiddle seems to be doing exactly what it should to me. When you click on the checkbox, it's not in the menu. It is now checked, so your javascript unchecks it.
0

A bit expressive, but I hope it will be more clear to the reader:

$(document).ready(function() {
  var $window = $(window);
  var $btn = $('.menu-btn');

  $window.on('click', function (event) {
    var $target = $(event.target);

    if($target.is($btn)) {
      return;
    }

    if($btn.is(':checked') === false) {
      return;
    }

    if ($target.closest('.menu').length > 0) {
      return;
    }

    $btn.prop('checked', false);

  });
});

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.