- This topic has 5 replies, 4 voices, and was last updated 3 years, 5 months ago by
David.
-
AuthorPosts
-
December 22, 2020 at 2:17 pm #1591249
Mikko
Hi,
I would like the navigation search close when tapped outside of it.
I asked similar question regarding closing the mobile navigation.
https://generatepress.com/forums/topic/close-mobile-menu-when-click-tap-outside/
Tom then provided this code:
document.addEventListener( 'click', function( evt ) { var openedMenu = openedMenu = document.querySelector( '.toggled .menu-toggle' ), targetElement = evt.target; // clicked element do { if ( targetElement == openedMenu ) { // This is a click inside. Do nothing, just return. return; } // Go up the DOM targetElement = targetElement.parentNode; } while ( targetElement ); // This is a click outside. openedMenu.click(); } );Would it be possible to edit this code so that it applies to navigation search too or if separate code is required I wouldn’t mind using it either.
Thank you!
December 22, 2020 at 7:38 pm #1591449Elvin
StaffCustomer SupportHi,
We can add in one for the search:
document.addEventListener( 'click', function( evt ) { var searchForm = document.querySelector( '.nav-search-active' ); var openedSearch = openedSearch = document.querySelector( '.close-search' ), targetElement = evt.target; // clicked element do { if ( targetElement == openedSearch || targetElement == searchForm ) { // This is a click inside. Do nothing, just return. return; } // Go up the DOM targetElement = targetElement.parentNode; } while ( targetElement ); // This is a click outside. openedSearch.click(); } );I’ve tested it along with Tom’s code and it seems to be working on my end. It should work for yours as well.
Let us know.
December 23, 2020 at 1:30 pm #1592529Mikko
Works like a charm! Thank you.
December 27, 2020 at 3:10 pm #1596065Elvin
StaffCustomer SupportNice one! Glad it worked for you. No problem. 🙂
November 2, 2022 at 7:32 am #2398293Hans
Hi,
When I use this script (added with an element) my search menu does not open, any suggestions how to fix this?
This website: https://veendambeweegt.nl/
Hans
November 3, 2022 at 2:46 am #2399598David
StaffCustomer SupportHi there,
do you want to remove that script and ill look at an alternative method.
-
AuthorPosts
- You must be logged in to reply to this topic.