Close DropDown Menu Outside Click
-
I have a mobile menu that doesn’t close on outside clicks. Ironically, the search does close on outside clicks. Here is the code:
mobileMenu: function() { var $closedSymbol = this.cache.$isRTL ? '◄' : '►'; if ( $.fn.slicknav != undefined ) { var $mobileMenuAlt = $( '.pwd-mobile-menu-alt ul' ); var $menu = $mobileMenuAlt.length ? $mobileMenuAlt : $( '.pwd-site-nav-container .pwd-dropdown-menu' ); if ( $menu.length ) { $menu.slicknav( { appendTo : '.pwd-site-header', label : pwdLocalize.mobileSiteMenuLabel, allowParentLinks : true, closedSymbol : $closedSymbol } );This is the code for the Search:
headerSearch: function() { var $headerSearch = $( '.pwd-site-header-search' ), $headerSearchToggle = $( '.pwd-search-toggle' ); $headerSearchToggle.click( function() { $headerSearch.toggleClass( 'pwd-active' ); return false; } ); this.cache.$document.on( 'click', function( event ) { if ( ! $( event.target ).closest( $headerSearch ).length ) { $headerSearch.removeClass( 'pwd-active' ); } } );What could I add to make this work? Or can I take from the search code and replace with the correct placements? And this is just for the mobile menu. Thanks!
-
I’m not sure how slicknav opens a mobile menu to begin with. It’s likely by adding a specific class attribute to the menu container. You need to determine how this is done so it can be undone by an outside click event. You can watch the element inspector area of your browser’s mobile emulator developer tool to find out what changes on menu open/close.
You cannot just collect any document click to close a menu because it’ll inhibit submenu navigation. That’s still the first step, but then determine if the click is within the menu element or not before changing the class attribute. More here:
https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element/3028037#3028037
The topic ‘Close DropDown Menu Outside Click’ is closed to new replies.