How to close mobile menu when using Page scroll to id plugin
In order to auto-close an opened mobile menu when links that scroll the page are clicked, you’ll need to add some extra javascript in your page or theme template.
This issue is not a problem that comes from “Page scroll to id”, as the plugin has no control over the mobile menu a site is using. The same problem would happen even if you were using other similar plugins or no plugin at all (e.g. the page would “jump” but the mobile menu would still be opened). This issue comes from the fact that most mobile menu scripts do not auto-close the menu, when menu links are clicked, simply because most developers forget that a link might point to an anchor within the same page.
I try to support such issues in order to help users that use “Page scroll to id” plugin, deal with the problem quickly. I typically provide a jQuery/js script that users need to add to their page. Each script is different for each site and created specifically for a single mobile menu.
I can do this simply because I know javascript and I can find quickly how other scripts (like mobile menu script) work. I provide the script(s) for free, but if I help you and you can make a donation, I’d greatly appreciate it 🙂
Where to put the extra script
If your theme provides a field to add javascript code in your theme, you can place the code there. Otherwise, you can simply add the script in your theme/child-theme footer.php
template, right before the closing body tag (</body>
).
How to request such script
Please leave a comment with the URL of your site/page you need help with and I’ll try to provide the script as quickly as possible.
A typical script looks like this:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#mobile-menu-selector .menu-item ._mPS2id-h",function(){ $("#mobile-menu-close-btn-selector").trigger("click"); }); }); })(jQuery); </script>
If you’re using “Responsive Menu” plugin (by ExpressTech), you can use this code provided by one of our users.
Can I get support if I don’t use “Page scroll to id” in my site?
You can make a donation and request support 😉
Hi –
I think this is my issue as well? swiresound.com is the website. Kudos if you figured it out… was struggling for quite some time
Yes it is 🙂
This script should do the trick:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#site-navigation.toggled-on .menu-item .__mPS2id",function(){ $("#site-navigation .menu-toggle").trigger("click"); }); }); })(jQuery); </script>
Hello! Please help me.
Plugin “Page scroll id” works correctly if install check in settings: Prevent other scripts from handling plugin’s links (if possible). Then PC version is ok. But mobile version has a problem with a closing menu with links #id type.
thanks
my website:
https://bca-group.ru/
Hello,
Try adding the following script:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#mysticky-nav .mobile-icon .menu-item .__mPS2id",function(){ $("#mysticky-nav .mobile-icon .active-menu").trigger("click"); }); }); })(jQuery); </script>
Let me know if it works
Very good job! You are super hero! Very, very good! Thank you very much! Mobile menu closing is correctly! Be happy!
Glad I helped 🙂
Hi!
Would you help me with the script please on my side http://www.themomentscottage.com
And where can i put it in my side – i don´t have a child theme …
I already made a donation.
Many Thanks,
Josef
Hello,
Try this:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#modal-1-content a.__mPS2id",function(){ $("button[data-wp-on-async--click='actions.closeMenuOnClick']")[0].click(); }); }); })(jQuery); </script>
Let me know if it works.
Thanks for your donation 🙂
Hi Malihu, can you help me?
I’m using WP bakery on Total theme. :/
Thank you a lot!
One clarification, I use the child theme 🙂
Hello,
I provide free support only for “Page scroll to id” plugin users. If you don’t want to use the plugin and you still want me to help you with the code, you can make a small donation. You can contact me here if you want 🙂
Hi Malihu. Love your theme and all the setting options. Very robust. Exactly what I needed. Would greatly appreciate your help with the script that will close the mobile menu after selecting a menu #link. I’m using The7 theme with WPBakery. Many thanks!
Hi, try this:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#mobile-menu .menu-item .__mPS2id",function(){ $(".dt-close-mobile-menu-icon").trigger("click"); }); }); })(jQuery); </script>
Let me know if everything works
Your script works perfectly. The menu on my iPhone now closes automatically when a #link menu option is selected. Thanks so much!
Hi! Any chance you can provide me the code snippet to fix this?
I have tried adding:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#wp-block-navigation-item .menu-item ._mPS2id-h",function(){ $("#wp-block-navigation__responsive-container-close").trigger("click"); }); }); })(jQuery); </script>
As you can see but it does not seem to be working. (Above code was a wild guess, I don’t know much about JS)
Hello,
Did you fix this? I tested your page and the mobile menu closes as expected.
Hello,
my mobile menu will not close after click the link “scroll to the id” , if the link is on the same page. Please could you help?
In advance thank you very much.
Best regards
Betti
And sorry, I don’t know where to write the code .
I see the footer.php in my theme is totally empty.
So I think something is missing there, like : … Pöease could you tell me the corrct way to place the code. Thanks sooo much.
Hello Betti,
This is the code you need to use:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#menu-mainmenunewmobil .menu-item .__mPS2id",function(){ $("#mobile-menu a.close").trigger("click"); }); }); })(jQuery); </script>
Your footer.php should not be empty… maybe you use a child-theme?
Can you edit your theme/child-theme functions.php? If yes, you can add the following:
function ps2id_custom_script(){ wp_register_script( 'ps2id-custom-script', '', array('jquery', 'page-scroll-to-id-plugin-script'), '', true ); wp_enqueue_script( 'ps2id-custom-script' ); wp_add_inline_script( 'ps2id-custom-script', '(function($){ $(window).on("load",function(){ $(document).on("click","#menu-mainmenunewmobil .menu-item .__mPS2id",function(){ $("#mobile-menu a.close").trigger("click"); }); }); })(jQuery);'); } add_action( 'wp_enqueue_scripts', 'ps2id_custom_script' );
Let me know 🙂
Hi there,
I would love your help with this. I have tried fiddling with the example scripts but haven’t found the right selectors yet.
Really appreciate your help.
Warmly,
Emily
Hello Emily,
Try the following and let me know if everything works:
<script> (function($){ $(window).on("load",function(){ $(document).on("click",".wp-block-navigation__responsive-container .__mPS2id",function(){ $(".wp-block-navigation__responsive-container-close")[0].click(); }); }); })(jQuery); </script>
Thank you so much Malihu! It works!
You’re welcome Emily. Glad I helped 🙂
Hi malihu,
could you help me please for my website: fajdiga.com
I have also the problem with the mobile menu when using Page scroll to id plugin.
best regards
Marin
Hello Marin,
Try this script:
(function($){ $(window).on("load",function(){ $(document).on("click","#navbarNavDropdown.collapse.show .menu-item ._mPS2id-h",function(){ $(".navbar-toggler[data-target='#navbarNavDropdown']").trigger("click"); }); }); })(jQuery);
Also, there’s a console error coming from “TC Custom JavaScript”, probably because you don’t need to add the script tags in there. Just copy/paste the code above without any
<script></script>
.Let me know if everything works
Hello Malihu,
I tried it, works perfectly.
Thanks for your help.
BR,
Marin
You’re welcome 🙂
Hello, could someone please help me with modification for my website ? My mobile menu also doesnt auto close after clicking. I would be really greatful.
The website:
https://www.astradigital.sk/
Hi,
I provide free support only for “Page scroll to id” plugin users. You can make small donation if you want me to help you regardless (i.e. without using the plugin). You can contact me here if you want.
Hi, please, could you help me to adapt this code to my website ?
For mobile when I click on menu, the menu does not close. Please help me wuth this jquery script.
Thanks
Hello,
I provide free support only for “Page scroll to id” plugin users. You can make small donation if you want me to help you regardless (i.e. without using the plugin). You can contact me here if you want.
Hi Malihu,
Could you help me please for my website:
https://www.studiobtrenton.net/
I have also the problem with the mobile menu when using Page scroll to id plugin.
Thanks in advance, hope you can help.
Eric
Nevermind I managed to figure it out. I used this code for the sinatra them
console.log(“active”)
document.getElementById(‘menu-item-129’).onclick = function(){
console.log(“triggered”);
sinatraTriggerEvent(document,”sinatra-close-mobile-menu”);
}
document.getElementById(‘menu-item-128’).onclick = function(){
console.log(“triggered”);
sinatraTriggerEvent(document,”sinatra-close-mobile-menu”);
}
Would anyone be able to adjust the code to suit my website? I have a page anchor on the home page for ‘Functions & events’ but the overlay menu stays ontop if on the homepage already.
Thank you!
Hello! Could you please help with the code for my site?
Kaliméra Manos,
I thought I could adapt your script by myself, but failed. Could you please provide support for https://www.innersource.at
🙏🏼
Hi malihu,
could you help me please for my website: kinderwalderlebnis.de
I have also the problem with the mobile menu when using Page scroll to id plugin.
best regards
Tobias
Hello Tobias,
This should do the trick:
<script> (function($){ $(window).on("load",function(){ $(document).on("click",".mobile-header-item .menu-item .__mPS2id",function(){ $(".mobile-menu-close")[0].click(); }); }); })(jQuery); </script>
Let me know if it worked
Dear malihu,
thank you very much, it works.
all the best
Tobias
You’re welcome 🙂
Hi,
I tried to replace your code with the right declarations but cnn’t find to good ones.
Can you provide me with the working code for https://arjennedaams.nl.
Many thanks in advance.
Sure.
Because of the way your menu works, it needs an extra condition to trigger the click event (which is not triggered via jQuery by the way):
(function($){ $(window).on("load",function(){ $(document).on("click","#site-navigation .menu-item .__mPS2id",function(){ if($("h3.menu-toggle").is(":visible")) $("h3.menu-toggle")[0].click(); }); }); })(jQuery);
In the code above we basically make sure that the hamburger menu icon is visible before we trigger the close menu, otherwise it interferes with the desktop menu version. We also trigger a pure js click event (instead of jQuery) because that’s the event your mobile menu uses.
Let me know 🙂
Thanks for your quick response. It works perfect now!
One other question.
I set the offset on “.site-header”. Works perfect on desktop, but on mobile there is still some space. Is it possible to make different declarations for desktop and mobile?
Since your site’s header is not sticky/fixed, it’s better to use plugin’s
:fixed
offset selector expression. So, in plugin settings, change the “Offset” option value to:.site-header:fixed
Save changes, test and let me know if it works as you want it.
Unfortunately it doesn’t work.
Maybe it’s because the “#masthead” is already fixed?
I also tried it with #masthead:fixed” but that doesn’t do the trick too
I checked your page and the option is still
.site-header
. Did you changed it to.site-header:fixed
? Maybe you need to clear/flush cache?It stille doesn’t work 🙁
I cleared the cache en checked in several browsers, but no result. I think I (have to) be satisfied with the current situation. Which isn’t really that bad either.
I made a small donation for your help.
Thanks a lot for your donation 🙂
The updated “Offset” option value is still not applied on the front-end.
Leave the “Offset” option value to
.site-header:fixed
and check again tomorrow. Maybe there’s some server cache that needs to be flushed(?). Do you have a caching plugin or CDN service on your site?Alternatively, we could use the following way to change the offset value programmatically:
http://manos.malihu.gr/page-scroll-to-id-for-wordpress/2/#faq-24
Let me know if you wanna try it.
Hi, I use beaver builder theme and the page scroll to id works very well on desktop, but on mobile when click on a link doesn’t close the menu.
I tried to use the javascript code that you posted here, but when I put on beaver builder child theme it says that “script” is an “unclosed regular expression”, I tried to erase the “” but does not works too.
Could you help?
“I tried to erase the “script” but does not works”
Hello,
Please post your site/page URL so I can check it and help.
I can’t, is a website of a company, sorry.
But the site was made in beaver builder, and the menu links works well, just don’t close the menu when click on them.
There is a way to solve this?
But I think if translate the script in css, it will work, or no, I don’t know
(function($){
$(window).on(“load”,function(){
$(document).on(“click”,”#prk_menu_els .menu-item .__mPS2id”,function(){
$(“#prk_menu_left_trigger”).trigger(“click”);
});
});
})(jQuery);
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#prk_menu_els .menu-item .__mPS2id",function(){ $("#prk_menu_left_trigger").trigger("click"); }); }); })(jQuery); </script>
You can send send it to me privately via email here:
https://manos.malihu.gr/contact
Can you do this?
Unfortunately no, but the theme provides a field to add javascript code, but don’t allow to use the script, it says “HTML detected, the “JavaScript Code” setting only accepts Javascript.”
I tried to change to
</body>
but does not works too.Ok, try to add only the js code without the script tag (or any other tags):
(function($){ $(window).on("load",function(){ $(document).on("click","#prk_menu_els .menu-item .__mPS2id",function(){ $("#prk_menu_left_trigger").trigger("click"); }); }); })(jQuery);
Doesn’t works unfortunately. Do you think that if write the code in css would work?
Or is there other place to put the script? That certainly will work
Do not add it in CSS.
Yes, you can place the script in your theme template as described in the post:
Edit your theme/child-theme
footer.php
template (via ftp or the theme editor) and place the script (with the<script>
tags) below the line with thewp_footer()
function and before the closing body tag (</body>
).Save the file and test.
My child theme does not have anything, there is just “styles.css” and “functions.php”.
But on theme there is the “footer.php”.
I put the code there and updated, but does not works because I use the child theme, and on child theme doesn’t have the “footer.php” file.
I did exactly what you said, and appeared that the code was edited with success (on theme, not on child), but dosn’t works, it looks like a joke but doesnt.
You can either a)add the code directly to the footer.php in the theme’s directory or b)you can copy your theme’s footer.php in your child-theme directory and add the code to the newly copied footer.php (in the child-theme folder).
People usually prefer the second way (b) so they don’t have to redo the changes if/when they update their theme.
The problem is unless I see your page’s HTML and classes, I cannot say which selector to add in the js script. It’s not possible to provide a js code without visiting your page/site and seeing the markup, classes and id attributes of your theme’s mobile menu.
You’ll need to find your mobile menu selector and set it in the js code like this:
$(document).on("click","mobile-menu-selector-here .menu-item .__mPS2id",function(){
and you need to find your mobile menu close button selector and set it in the js code like this:
$("mobile-menu-close-button-selector-here").trigger("click");
Well, the “a” option I’ve already did and doesn’t works, I put the code exactly before the body tag.
The “b” option, I don’t know, because on child theme there is almost nothing, just have the “functions.php” and “styles.css”, and on them are practically blank, have just few lines of code, and on “functions.php” is almost empty.
I didn’t find the mobile menu selector code, do you know in wich file is it? Because I didn’t find a file named “menu…js/php”
In wich file is to paste the footer.php on child theme, functions.php? But I erase everithing there or I just paste after of what’s there, or before?
The almost empty is the style.css file, on functions.php there is a few lines saying something about directory
If you added the js code in your theme’s footer.php (a) and it doesn’t work, it’s because you need to change the selector(s) in the script to match your mobile menu.
In order to find the selector(s) you need to open your page with browser’s developer tools and see the id and classes it has. But this is not something you can do if you don’t have some experience with web development.
In short, the script provided in this post is not a global solution that works everywhere. That’s why I first visit the site and then provide the code to the user to add it.
Is the mobile menu part of your theme or another plugin? Which theme are you using?
I use the beaver builder theme (child-theme) and beaver builder plugin, but my menu is from the beaver builder plugin, not from theme.
When you click on create a page (using beaver plugin), there is a + at the top right, and when you click appear many “options” (modules) and my menu is on the first page, (Modules/standard modules/actions/menu).
The config that I put on responsive menu is:
Responsive Toggle: Hamburger Icon,
Responsive Style: Flyout Overlay,
Flyout Position: Right,
Responsive Breakpoint: Medium & Small Devices Only,
Sub-menu Item Icon: None.
Try changing the script you added to your theme’s
footer.php
to:<script> (function($){ $(window).on("load",function(){ $(document).on("click",".menu-item .__mPS2id",function(){ $(".fl-menu-mobile-close").trigger("click"); }); }); })(jQuery); </script>
Save and test.
Doesn’t works, but
I found the mobile menu code
<div class="pp-advanced-menu-mobile-toggle hamburger"> <div class="pp-hamburger" tabindex="0" role="button" aria-label="Menu"><div class="pp-hamburger-box"><div class="pp-hamburger-inner"></div></div></div> </div> <div class="pp-advanced-menu pp-advanced-menu-accordion-collapse off-canvas"><div class="pp-clear"></div> <nav class="pp-menu-nav pp-off-canvas-menu pp-menu-right" aria-label="Menu" itemscope="itemscope" itemtype="https://schema...."> <a href="javascript:void(0)" class="pp-menu-close-btn" aria-label="Close the menu" role="button">×</a> <ul id="menu-menu-1" class="menu pp-advanced-menu-horizontal pp-toggle-none"><li id="menu-item-1" class="menu-item menu-item-type-custom menu-item-object-custom"><a href="#about" data-ps2id-api="true"
I think it can helps
Where is my comment
Your comment was falsely held for moderation. I fixed it.
Try changing the js code to:
<script> (function($){ $(window).on("load",function(){ $(document).on("click",".menu-item .__mPS2id",function(){ $(".pp-menu-close-btn").trigger("click"); }); }); })(jQuery); </script>
or
<script> (function($){ $(window).on("load",function(){ $(document).on("click",".menu-item .__mPS2id",function(){ $(".pp-menu-close-btn")[0].click(); }); }); })(jQuery); </script>
and see if it works
Doesn’t works, I went to customize (theme) and code>footer code and put the code there, and none worked,
I first put the first, publish and nothing, so I erase, put the other code, and publish again, and nothing too.
What can I do to solve this?
Hi, After searching for half a day, I stumbled on this page. First of all, brilliant plugin!
But like many others the mobile hamburger menu does not dissappear after clicking on a …#link
Would appreciate if you can help me out!
Cheers, Eddie
Hi,
Try this script:
<script> (function($){ $(window).on("load",function(){ $(document).on("click","#prk_menu_els .menu-item .__mPS2id",function(){ $("#prk_menu_left_trigger").trigger("click"); }); }); })(jQuery); </script>
Thank you for your comments 🙂
Thanks so much. I did have to look trough previous posts where to put this. I have a Javascript option provided in the template which did not work. So I read is should be in footer.php and then it worked like expected!
Cheers!
You’re welcome 🙂
Hi there, it’s so awesome that you’re helping everyone out. I have tried myself but with my limited Javascript knowledge I just can’t come right.
My website that I’m trying on is:
https://africaaviationacademy.co.za/africa-aviation-academy-home-page/
Regards
Hi,
I provide free support only for “Page scroll to id” plugin users. You can make small donation if you want me to help you regardless (i.e. without using the plugin). You can contact me here if you want.
Hi could you help me as well with the script?
Thank you!
hacienda.cristitinca.ro
Sure, try this:
<script> (function($){ $(window).on("load",function(){ $(document).on("click",".nd_options_navigation_4_sidebar_content .menu-item .__mPS2id",function(){ $(".nd_options_close_navigation_4_sidebar_content").trigger("click"); }); }); })(jQuery); </script>
Thank you, it worked!
You’re welcome! Glad I helped 🙂
Hi there,
My website is formstechnology.gr.
Could you please provide me the correct script ?
Best regards
Γεια χαρά,
This script is for sites/pages using “Page scroll to id” plugin and I provide free support for the plugin.
If you want me to help with your site regardless of using the plugin or not, you can contact me using the link below and I’ll give you a quote (e.g. if you just want to close the mobile menu a small donation would be ok):
http://manos.malihu.gr/contact