0

I am trying to implement a basic toggle menu after the last has stopped working but no matter how much I try to narrow down the CSS selector, the sub menu items ALWAYS toggle the menu on click for the mobile menu and therefore you cannot click through.

My JS is:

jQuery(document).ready(function() {
  jQuery('#menu-item-1837').click(function(e) {
      jQuery('#menu-item-1837 > ul').slideToggle(500);

      e.preventDefault();
  });
   
});

jQuery(document).ready(function() {
  jQuery('#menu-item-1832').click(function(e) {
      jQuery('#menu-item-1832 > ul').slideToggle(500);

      e.preventDefault();
  });
   
});

I have tried all sorts of variations including targeting using:

#menu-item-1837 a:first-child

.menu-item-has-children > a 

a#menu-item-1837

I can't work out why it isn't working

I have also tried variations of targeting the sub-menu but it never works

Any help would be appreciated

Mr Toad
  • 202
  • 2
  • 12
  • 41
  • So a click on a menu item closes the whole menu? Look for [stopPropagation()](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation). – Louys Patrice Bessette Apr 07 '21 at 23:42
  • Clicking on the parent open the sub menu as expected, but then clicking a sub menu item closes it again instead of taking you to the link – Mr Toad Apr 07 '21 at 23:43
  • 1
    The click handler on the menu item is propagating... Up to the click handler for the menu open/close. Check the link I suggested above... I feel that is the problem. – Louys Patrice Bessette Apr 07 '21 at 23:45
  • 1
    Oh wow - that fixed it! I didn't actually know how to implement it so i took a stab and replaced the line e.preventDefault(); with event.stopPropagation(); and that fixed it Thank you so much – Mr Toad Apr 07 '21 at 23:48
  • Here is a duplicate question about this: [What's the difference between event.stopPropagation and event.preventDefault?](https://stackoverflow.com/questions/5963669/whats-the-difference-between-event-stoppropagation-and-event-preventdefault) – Louys Patrice Bessette Apr 07 '21 at 23:53
  • Can you share the site link? – Bhautik Apr 08 '21 at 06:17

0 Answers0