Uncheck a checkbox when clicking outside it

4


HowcanIunmarkacheckboxifIclickonanotherfieldinthesite?

Thecheckboxissothatwhenthestateischecked,it'drags'mymenufrom-65%to0andappearsonthescreen,beingusedinaresponsivemobilelayout.

UnfortunatelyIhavenoideahowthiscanbedone,butIbelieveit'sinjavascript/jquery.

Ex:

<headerid="menuCelular">
<input type="checkbox" id="btn-menu">
<label id="barra-icon-menu" for="btn-menu"><label id="txt-menu" for="btn-menu">Menu</label></label>

<nav class="menuMobile">
<ul class="menu">

      <li id="mapa-menu" class="abre-dropdown"><a href="\">Mapa do site</a>
        <ul class="submenu-1">
            <li><a href="\">link</a></li>
            <li><a href="\">link</a></li>
            <li><a href="\">link</a></li>
        </ul>
    </li>    
   <li class="abre-dropdown"><a href="\">>Aprendizado</a>
        <ul class="submenu-1">
            <li><a href="\">link</a></li>
            <li><a href="\">link</a></li>
            <li><a href="\">link</a></li>
        </ul>
    </li>
    <li class="textos-menu"><a href="\" title="\">Textos</a></li>    
    <li class="fotos-menu"><a href="\" title="\"></label>Fotos</a></li> 

</ul>
</nav>
</header>

Now when the menu is open, if I click bar #menuCell it does not close, unless I press outside the .menuMobile region

SITE (Resize viewport for 479px)

    
asked by anonymous 29.12.2015 / 18:43

3 answers

6

Well if you really want to use checkBox , you can do so:

var checkbox = $('#button-menu');

$('body').not('#menu').click(function() {
  checkbox.prop("checked", false);
});

$('#button-menu, #menu').click(function(event) {
  event.stopPropagation();

});
#geral {
  width: 200px;
  height: 200px;
  background: #333;
}

#menu {
  width: 200px;
  height: 100px;
  background: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="geral">
  <input type="checkbox" id="button-menu">
  <nav id="menu">
    Clique fora dessa div

  </nav>
</div>

I used .stopPropagation() . Basically, clicking anywhere in body will cause checkbox to be deselected, however, if you click on the checkbox or div#menu the propagation of the event that makes the deselection will stop happening, so there may be the marking.

This method is very powerful so be careful when applying it. See here - link .

    
29.12.2015 / 19:26
2

Using the response and the link provided by @SamirBraga, follow code below without using event.stopPropagation();

var checkbox = $('#button-menu');

$(document).on('click', function(event) {
  if (!$(event.target).closest('#button-menu, #menu').length) {
    checkbox.prop("checked", false);
  }
});
#geral {
  width: 200px;
  height: 200px;
  background: #333;
}

#menu {
  width: 200px;
  height: 100px;
  background: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="geral">
  <input type="checkbox" id="button-menu">
  <nav id="menu">
    Clique fora dessa div

  </nav>
</div>

The example was taken from the own link about the dangers of using stopPropagation , which according to it should only be used when you really want to cancel an event from the DOM tree, such as submitting a form.

  

Stopping propagation should be thought of as canceling an event, and   it should only be used with that intent. Maybe you want to prevent   form submission or disallow focus to an area of the page. In these   you are stopping propagation because you do not want to happen   happen, not because you have an unwanted event handler registered   higher up in the DOM.

    
29.12.2015 / 21:05
1

Try this:

$("#geral").not("#menu, #barra-icon-menu").click(function(){
    $("#button-menu").prop("checked", false);
    $("#barra-icon-menu").click();
});
    
29.12.2015 / 18:58