Skip to content
Advertisement

jQuery collapsible through toggleClass on element

I have an off canvas navigation menu (classname that is enabled via hover-over using jQuery on my WordPress site. It is, as it should be, not visible on page load.

For the mobile version, I want the same nav menu to be activated by clicking on a menu icon (that I’ve given two classes, in this order: mobile-nav-toggle show-nav-mobile). The toggle method seems to only work for a vertical toggle. My solution to replicating the same animation on click rather than hover, is by using the toggleClass method on the icon to toggle between a classname that opens the menu nav (show-nav-mobile) and closes it (hide-nav-mobile) Using the below code:

JavaScript

That doesn’t seem to do the job though. The jQuery opens the offcanvasmain div just fine, but doesn’t close it again.

What is wrong with my approach?

Advertisement

Answer

I assume your element initially looks somewhat like this:

JavaScript

This means that

a) Both these click handlers will always run when clicking, no matter if the element still has the class hide-nav-mobile:

JavaScript

jQuery finds the element at the moment you define the click handler; it doesn’t recheck if the element still has this class when clicking later.

b) This never attaches a click handler:

JavaScript

because at the time of calling jQuery(".show-nav-mobile") it cannot find any element with that class.

To fix it, do this all in a single click handler:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement