JavaScript Jquery Click this or that and toggle class jquery click this value,jquery click this element,jquery click

Im stuck on toggeling a class when there is a click event on either the parent or child item. It is about a navigation menu that can be clickable on the entire bar or just the icon.

Current situation: (according to the fiddle) When you click on the red block, the class is-active will be toggled on the .hamburger-menu class. Allowing the icon to transition via CSS. But when you click on the hamburger icon it does not toggle the class.

How can the class is-active be toggled when there is a click event on either the #menuContainer or the .hamburger-menu div?

jQuery(function($) {
  $(document).ready(function() {
    $("#menuContainer, .hamburger-menu").click(function() {
      $(".hamburger-menu").toggleClass("is-active");
    });
  });
});
.menu_container {
  display: block;
  position: absolute;
  left: 45%;
  top: 45%;
  width: 100px;
  height: 100px;
  background-color: red;
  cursor: pointer;
}

.menu_container .menu-active {
  background-color: blue;
}

.hamburger-menu {
  display: inline-block;
  position: absolute;
  left: 35%;
  top: 45%;
  margin: 0 auto;
  -webkit-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.hamburger-menu:hover {
  cursor: pointer;
}

.hamburger-menu span {
  display: block;
  width: 40px;
  max-width: 100%;
  height: 3px;
  background-color: #414042;
  margin-bottom: 5px;
  border-radius: 5px;
  -webkit-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.hamburger-menu.is-active span:nth-child(2) {
  display: none;
}

.hamburger-menu.is-active span:nth-child(1) {
  -webkit-transform: translateY(4.5px) rotate(45deg);
  -ms-transform: translateY(4.5px) rotate(45deg);
  -o-transform: translateY(4.5px) rotate(45deg);
  transform: translateY(4.5px) rotate(45deg);
}

.hamburger-menu.is-active span:nth-child(3) {
  -webkit-transform: translateY(-4.5px) rotate(-45deg);
  -ms-transform: translateY(-4.5px) rotate(-45deg);
  -o-transform: translateY(-4.5px) rotate(-45deg);
  transform: translateY(-4.5px) rotate(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menuContainer" class="menu-trigger menu_container">
  <div class="hamburger-menu">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
Answer:1

The issue is because you've added the click event handler to two elements, #menuContainer and .hamburger-menu. The latter is a child of the former, so when you click that the handler fires twice. This means that the class is toggled on, then immediately off again (or vice versa)

To fix this issue, call stopPropagation() on the event. This stops the event bubbling up the DOM and being handled twice.

jQuery(function($) { // Note you only need one document.ready handler here
  $("#menuContainer, .hamburger-menu").click(function(e) {
    e.stopPropagation();
    $(".hamburger-menu").toggleClass("is-active");
  });
});
.menu_container {
  display: block;
  position: absolute;
  left: 45%;
  top: 45%;
  width: 100px;
  height: 100px;
  background-color: red;
  cursor: pointer;
}

.menu_container .menu-active {
  background-color: blue;
}

.hamburger-menu {
  display: inline-block;
  position: absolute;
  left: 35%;
  top: 45%;
  margin: 0 auto;
  -webkit-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.hamburger-menu:hover {
  cursor: pointer;
}

.hamburger-menu span {
  display: block;
  width: 40px;
  max-width: 100%;
  height: 3px;
  background-color: #414042;
  margin-bottom: 5px;
  border-radius: 5px;
  -webkit-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.hamburger-menu.is-active span:nth-child(2) {
  display: none;
}

.hamburger-menu.is-active span:nth-child(1) {
  -webkit-transform: translateY(4.5px) rotate(45deg);
  -ms-transform: translateY(4.5px) rotate(45deg);
  -o-transform: translateY(4.5px) rotate(45deg);
  transform: translateY(4.5px) rotate(45deg);
}

.hamburger-menu.is-active span:nth-child(3) {
  -webkit-transform: translateY(-4.5px) rotate(-45deg);
  -ms-transform: translateY(-4.5px) rotate(-45deg);
  -o-transform: translateY(-4.5px) rotate(-45deg);
  transform: translateY(-4.5px) rotate(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menuContainer" class="menu-trigger menu_container">
  <div class="hamburger-menu">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
Answer:2

I am trying to pass in variable elementIDs from HighCharts label elements into a javascript function, using these I would like to launch the required divs in a jquery modal window. I have my example ...

I am trying to pass in variable elementIDs from HighCharts label elements into a javascript function, using these I would like to launch the required divs in a jquery modal window. I have my example ...

I have a ReactJS project where i get JSON from REST Django host and creating a table with filters for it. I have a Table class : class MainTable extends React.Component { constructor(props) { ...

I have a ReactJS project where i get JSON from REST Django host and creating a table with filters for it. I have a Table class : class MainTable extends React.Component { constructor(props) { ...

  1. react mount component only once

I am trying to change the icon on collapse and facing an event firing issue The HTML <div id="collapse-menu" class="row"> <div class=" panel"> <a href="#...

I am trying to change the icon on collapse and facing an event firing issue The HTML <div id="collapse-menu" class="row"> <div class=" panel"> <a href="#...

  1. bootstrap collapse event handler
  2. bootstrap collapse event not firing
  3. bootstrap collapse event angular
  4. bootstrap collapse event listener
  5. bootstrap collapse event
  6. bootstrap collapse event example
  7. bootstrap collapse event not working
  8. bootstrap collapse event get element
  9. bootstrap collapse event show
  10. bootstrap 4 collapse event
  11. bootstrap 3 collapse event
  12. bootstrap panel collapse event
  13. bootstrap accordion collapse event
  14. bootstrap collapse click event
  15. bootstrap navbar collapse event
  16. bootstrap collapse javascript event
  17. bootstrap after collapse event
  18. bootstrap collapse open event
  19. bootstrap data-toggle collapse event
  20. vue bootstrap collapse event

I want to get all selected values of bootstrap-multiselect on button click. My JSP page code is given below <script type="text/javascript"> $(document).ready(function() { $('#...

I want to get all selected values of bootstrap-multiselect on button click. My JSP page code is given below <script type="text/javascript"> $(document).ready(function() { $('#...

  1. javascript bootstrap multiselect
  2. javascript multiselect bootstrap
  3. js bootstrap multiselect
  4. vue js bootstrap multiselect
  5. js/bootstrap-multiselect.js download
  6. js/bootstrap-multiselect.js cdn
  7. angularjs bootstrap multiselect
  8. angularjs bootstrap multiselect dropdown