Uncheck a checkbox by clicking outside of it

Asked

Viewed 838 times

4

inserir a descrição da imagem aqui
How is it possible to uncheck a checkbox if I click another field of the site?

The checkbox is used so that when the status is 'checked', it 'drags' my menu from -65% to 0 and appears on the screen, being used in a mobile responsive layout.

Unfortunately I have no idea how this can be done, but I believe it is in javascript/jquery.

Ex:

<header id="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 #menuCellular bar it does not close unless I press outside the . menuMobile region

SITE (Resize the viewport to 479px)

  • Tried the method $.blur of jquery, I think adding it to the checkbox and assigning a function works

  • Can you give an example? I never used the method.

  • Probably the checkbox is not the best solution in this case. It would be better something related to the focus, or even links to fragment ( # ) + css.

  • Yes, I imagined it. It turns out it was the first responsive menu I did, so I got a little confused about all this, it’s new to me. I will edit and leave the site for you to look at. If you visit, resize your browser to see the mobile version (479px).

  • @Thiagobarros the hidden checkbox is very interesting for things with behavior toggle (on/off), but it’s just that in your case, it would be more related to the same focus. But it’s good to know how to use css + check, you can do some more complex things.

  • Yes, I learned the method with some video lessons from youtube. Anyway. it is not possible to make the following structure: Imagine that the checkbox has been marked, then: If the user presses in some region outside the menu that was opened with the own checkbox, it is unchecked, and automatically the menu disappears, because it only opens if the checkbox is marked.

Show 1 more comment

3 answers

6


Well if you really want to use the 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>
<div id="geral">
  <input type="checkbox" id="button-menu">
  <nav id="menu">
    Clique fora dessa div

  </nav>
</div>

I used the .stopPropagation(). Basically, by clicking anywhere on body you will make the checkbox is unchecked, however, if you click either the checkbox or the div#menu the spread of the event that makes the cancellation will no longer happen, so there may be the marking.

This method is very powerful so be careful when applying it. See here - https://css-tricks.com/dangers-stopping-event-propagation/.

  • 1

    It worked perfectly. However, now another problem has occurred: When I click on the bar to close the menu it no longer works, and the menu only closes if I click outside the region.

  • I don’t know if I was clear, if I need to, I can explain otherwise @Samir

  • Do you have an isolated function to close the menu? If you have it try to apply it after the .stopPropagation().

  • No, it works together with the checkbox: when 'checked' the menu slides from -65% to 0 (the menu is hidden in the corner of the screen), and when unchecked it goes back to -65%.

  • You can show me this part of your code, I think I know what the problem is.

  • Show it like this. Would you rather I put it in the commentary or edit the question? Alias, if you think it necessary, I undo all this checkbox event and redo the onclick event, although I have no idea how.

  • That’s what I was thinking. I think I’d better edit the answer, get better to view

  • Let’s go continue this discussion in chat. I edited the answer.

Show 3 more comments

2

Using the answer and the link offered by @Samirbraga, follows below code without using the 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>
<div id="geral">
  <input type="checkbox" id="button-menu">
  <nav id="menu">
    Clique fora dessa div

  </nav>
</div>

The example was taken from own link on the dangers of using the stopPropagation, that according to the same should be used only when you really want to cancel an event of the DOM tree, as for example the submission of a form.

Stopping Propagation should be thought of like canceling an Event, and it should only be used with that Intent. Perhaps you want to Prevent a form Submission or disallow Focus to an area of the page. In These cases you’re Stopping Propagation because you don’t want an Event to happen, not because you have an unwanted Event Handler Registered Higher up in the DOM.

  • 1

    Perfect! Thank you, I hope you help other people.

1

Try it this way:

$("#geral").not("#menu, #barra-icon-menu").click(function(){
    $("#button-menu").prop("checked", false);
    $("#barra-icon-menu").click();
});
  • I tested it and I’m still the same. That whole blue bar of the image opens the menu, because it is a label with is directed to the checkbox, and when pressed it changes the css properties and drags the menu to the screen.

  • This blue bar has id class? if you have me pass that I improve my answer and we try something else

  • I’ll be going over the structure in question, all right? Thank you.

  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.