This website uses cookies to personalise ads and to analyse traffic ok
web design

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 befole 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 ๐Ÿ˜‰


63 Comments

Post a comment
  1. Max
    Posted on May 14, 2021 at 19:31 Permalink

    Hello,

    Luckily I came across your site, since it exactly describes my problem. I would really want to have the mobile and tablet navigation menu to close on click, because it is a one-pager with page scroll to id.

    I would really appreciate if you have time to provide me the correct javascript coding in order to do so. I have the page scroll to id plugin installed.

    Thank you in advance.

    Reply
    • malihu
      Posted on May 15, 2021 at 16:06 Permalink

      Hi,

      Try the following script:

      <script> (function($){ $(window).on("load",function(){ $(document).on("click","#mobile-menu .menu-item ._mPS2id-h",function(){ $(".mobile-sticky-header-overlay.active").trigger("click"); }); }); })(jQuery); </script>

      Let me know if it works ๐Ÿ™‚

      Reply
      • Max
        Posted on May 15, 2021 at 21:39 Permalink

        Thank you a lot, it works perfectly.

        Have a nice day!

        Reply
  2. karen C
    Posted on April 20, 2021 at 06:38 Permalink

    I’m so lucky to happen upon your site. I just created scroll to id tag for my “Support” menu on iamamaker.org and of course having an issue with the menu closing on mobile. Any help is appreciated! Thank you!

    Reply
    • malihu
      Posted on April 20, 2021 at 13:16 Permalink

      No problem ๐Ÿ™‚ Try this script:

      <script> (function($){ $(window).on("load",function(){ $(document).on("click","#main-nav-clone .menu-item ._mPS2id-h",function(){ $("#top-primary-nav a.showhide").trigger("click"); }); }); })(jQuery); </script>

      Also, in plugin settings, enable “Verify target position and readjust scrolling (if necessary), after scrolling animation is complete” option.

      Let me know

      Reply
  3. John
    Posted on April 11, 2021 at 01:54 Permalink

    Thanks for the template. It works like a charm!
    Here is the snippet for the “Responsive Menu” plugin in case someone needs it.
    https://wordpress.org/plugins/responsive-menu/

    <script> (function($){ $(window).on("load",function(){ $(document).on("click",".rmp-menu-item-link",function(){ $(".rmp_menu_trigger.is-active").trigger("click"); }); }); })(jQuery); </script>

    Reply
    • malihu
      Posted on April 11, 2021 at 13:57 Permalink

      Great! Thanks for posting this ๐Ÿ™‚

      Reply
  4. Luvi
    Posted on April 10, 2021 at 17:51 Permalink

    Hi! I’d need some help on the exact javascript to insert to close the menu when clicking on an anchor link.

    Here’s the page I need help with: hic.iamluvi.com

    Thanks a lot!

    Reply
    • malihu
      Posted on April 10, 2021 at 22:47 Permalink

      Sure. Try this script:

      <script> (function($){ $(window).on("load",function(){ $(document).on("click","#nv-primary-navigation-sidebar .menu-item ._mPS2id-h",function(){ $("button.navbar-toggle.active").trigger("click"); }); }); })(jQuery); </script>

      Let me know if it works

      Reply
      • Luvi
        Posted on April 15, 2021 at 15:11 Permalink

        It worked, thank you a lot!

        Reply
  5. Earid
    Posted on March 11, 2021 at 14:24 Permalink

    Hi, can you help me with my site? I’m unable to find a mobile selector to get it to work.

    Reply
    • Earid
      Posted on March 11, 2021 at 14:31 Permalink

      I have sorted it out. Thanks for your tutorial. It was a lifesaver.

      Reply
      • malihu
        Posted on March 11, 2021 at 18:16 Permalink

        You’re welcome ๐Ÿ™‚

        If you need more help let me know.

        Reply
  6. Matthew
    Posted on February 23, 2021 at 17:52 Permalink

    Hi Manos, If you could help me collapse my mobile menu so your fantastic plugin works on the phone too I would be so grateful. Thank you!

    Reply
    • malihu
      Posted on February 23, 2021 at 18:18 Permalink

      Sure but I need to see your site/page in order to help. Let me know ๐Ÿ™‚

      Reply
      • Matthew
        Posted on February 23, 2021 at 19:02 Permalink

        Thanks Manos! It’s just a test site and I didn’t really want to publish it before it’s up and running. It’s the same as the domain of my email address. Can you read that? Thanks again!

        Reply
        • malihu
          Posted on February 23, 2021 at 23:00 Permalink

          No problem.

          You could use the following script:

          <script> (function($){ $(window).on("load",function(){ $(document).on("click","#site-navigation.toggled-on .menu-item ._mPS2id-h",function(){ $("#site-navigation.toggled-on .menu-toggle").trigger("click"); }); }); })(jQuery); </script>

          Also, instead of using the page1, page2, page3 etc. in your links, you could use the pre-existing ids that wrap your sections content. These pre-existing ids are:

          post-8
          panel1
          panel2
          panel3
          panel4

          If you use the above targets, you should remove the .panel-image value you set in “Offset” option in plugin settings.

          Hope this helps

          Reply
          • Matthew
            Posted on February 23, 2021 at 23:20 Permalink

            This is wonderful! You’ve helped make my site so much better – thank you!

        • malihu
          Posted on February 24, 2021 at 01:26 Permalink

          Glad I helped ๐Ÿ™‚

          Reply
          • Matthew
            Posted on February 24, 2021 at 09:21 Permalink

            It most certainly has! I’ve sent a small donation to say thanks.

        • malihu
          Posted on February 24, 2021 at 18:10 Permalink

          Got it ๐Ÿ™‚ Thanks a lot Matthew!

          Reply
          • Matthew
            Posted on February 24, 2021 at 18:26 Permalink

            You are welcome!

  7. [email protected]
    Posted on February 11, 2021 at 19:16 Permalink

    Hi, could you send me the code to automatically close the menu when scrolling to an ID?

    Reply
    • malihu
      Posted on February 12, 2021 at 00:54 Permalink

      Hello,

      You don’t seem to be using “Page scroll to id” plugin. I offer free support for the plugin only.

      If you want me to help you without using the plugin, please send me an email and I’ll give you a quote (probably a small donation).

      Reply
  8. Jon
    Posted on November 9, 2020 at 12:04 Permalink

    Hi Mate,

    I made an attempt to code the JS myself using your template above, but I just can’t get it to work on this site, can you give me a hand? What I have at the moment is;

    (function($){
    $(window).on(“load”,function(){
    $(document).on(“click”,”.main-menu-modal .menu-item ._mPS2id-h”,function(){
    $(“.close-main-nav-toggle”).trigger(“click”);
    });
    });
    })(jQuery);

    Much appreciated ๐Ÿ™‚

    Reply
    • malihu
      Posted on November 9, 2020 at 15:51 Permalink

      Hi,

      The script you added seems to work for me (it closes the menu). Does it conflict with something else?

      Reply
      • Jon
        Posted on November 9, 2020 at 17:06 Permalink

        I can’t see any confiliction, honestly I can’t even see if it’s being called… I’ve tried sticking it in as a seperate js file, and now have it tagged onto the end of footer.php.

        Can you take a look and see if you can tell the issue?

        Thanks very much,

        Jon

        Reply
        • Jon
          Posted on November 9, 2020 at 17:11 Permalink

          Scratch that, I just needed to purge the WP cache XD

          Thanks bud.

          Reply
  9. Maximilian
    Posted on September 22, 2020 at 10:59 Permalink

    Can you give me the code for this page?
    https://beta.bzg.li/

    Reply
    • malihu
      Posted on September 22, 2020 at 20:06 Permalink

      Hi,

      This page (and the free support) is for “Page scroll to id” plugin, which you don’t seem to use in your site(?)

      Reply
      • Maximilian
        Posted on September 23, 2020 at 10:18 Permalink

        I had it installed, but I tried different things. Can you look over it?
        https://beta.bzg.li/

        If it works I will spend some money ๐Ÿ™‚

        Reply
        • malihu
          Posted on September 24, 2020 at 04:17 Permalink

          I just checked it and the mobile menu seems to be already closing when clicking the links.

          Reply
          • Maximilian
            Posted on September 24, 2020 at 09:59 Permalink

            But why is it going way to far? :/

        • malihu
          Posted on September 24, 2020 at 18:22 Permalink

          This functionality is part of your theme and/or Elementor page builder. When “Page scroll to id” is not installed, it seems to be scrolling to the correct place, right?
          If you want to use it with “Page scroll to id”, you’ll probably have to manually edit some scripts (you’ll need to activate the plugin in order to see what can be done).

          Reply
  10. Oneka
    Posted on September 17, 2020 at 16:19 Permalink

    Hi!

    Sorry to bother you. I don’t know if it’s exactly this topic, as I can close the menu by clicking anywhere, but it scrolls to the top again.

    I’ve tried checking the options you said in other comments (Append the clicked linkโ€™s hash value… and Prevent other scripts from handling pluginโ€™s links), but it doesn’t seem to work.

    Also, all the submenus stay highlighted instead of the selected one only, I don’t know if you know how to fix it, otherwise nevermind.

    Thank youuu!!

    Reply
    • malihu
      Posted on September 20, 2020 at 17:18 Permalink

      Hello,

      These issues you’re having come from your theme (the highlighted submenus is not really an issue).

      About submenu links staying highlighted:

      It’s common for themes to highlight current page menu items and that’s exactly what your theme does. To revert this, simply add the following to your additional CSS in customizer or to your stylesheet:

      .sf-menu > li li.current_page_item > a._mPS2id-h:not(.mPS2id-highlight):not(:hover), .sf-menu > li li.current-menu-item > a._mPS2id-h:not(.mPS2id-highlight):not(:hover), .sf-menu > li li.current_page_ancestor > a._mPS2id-h:not(.mPS2id-highlight):not(:hover), .sf-menu > li li.current-menu-ancestor > a._mPS2id-h:not(.mPS2id-highlight):not(:hover), .sf-menu > li li.current-menu-parent > a._mPS2id-h:not(.mPS2id-highlight):not(:hover){ color: #020202; }

      About the page scrolling to the top after closing the mobile menu:

      Sometimes theme developers neglect the idea that some links might point inside the same page and it seems that this is the case with your theme.
      The menu is coded in such way, that when it closes, it moves focus to the hamburger menu icon, which is at the top of the page, thus making the page scroll to the focused item, i.e. to the top.

      The script that does this is this:

      ...themes/amphibious/js/custom.js

      The only way to fix this is to manually edit this script file and modify the code.
      If you can do this, edit custom.js and find the lines 185-187 (commented as “Bring focus to menu control”) and change the code from:

      setTimeout( function() { $( '.toggle-menu-control' ).focus(); }, 300 );

      to:

      /* setTimeout( function() { $( '.toggle-menu-control' ).focus(); }, 300 ); */

      Save the file and test your page.

      Reply
  11. Vasilis
    Posted on July 3, 2020 at 06:25 Permalink

    Hi, I want to hide the mobile menu (which is actually the same in dekstop) when clicking the links that take you to different parts of the page using your plugin.
    Thanks a lot in advance!

    Reply
  12. Jigar Chaudhary
    Posted on June 8, 2020 at 22:31 Permalink

    Menu Not Closing and not scrolling properly. Please help me.

    Reply
    • malihu
      Posted on June 9, 2020 at 11:47 Permalink

      Your theme scrolls to the top every time you close the mobile menu. This can’t be fixed with the plugin but maybe the following could work:

      Go to “Page scroll to id” settings and enable “Append the clicked linkโ€™s hash value to browserโ€™s URL/address bar” option. Save changes and test your page.

      Reply
      • Jigar Chaudhary
        Posted on June 9, 2020 at 19:45 Permalink

        Please help me. I clicked that option also but it didn’t work. Please help me to solve this issue.

        Reply
        • malihu
          Posted on June 11, 2020 at 12:20 Permalink

          Did you solve the issue? Seems to be working for me now.

          You need to remove the www part from your links URL because your web-server doesn’t use it and redirects all requests.

          You also have an js console error which might prevent scripts from running (not sure where the error comes from).

          Reply
  13. igor
    Posted on May 28, 2020 at 00:31 Permalink

    I’m trying to use the code above by changing the variables, but it doesn’t work … Do I have to insert it after the end of the tag? Would you help me? I’m not quite sure how it works

    (function($){
    $(window).on(“load”,function(){
    $(document).on(“click”,”.menu .mobile_switch_on .show_sub_menu .menu-item ._mPS2id-h”,function(){
    $(“.mobile-hamburger-toggle .is-active”).trigger(“click”);
    });
    });
    })(jQuery);

    Reply
    • malihu
      Posted on May 28, 2020 at 05:42 Permalink

      You need to change your code because your close button uses the touchstart event (not click) and you also set your css selector wrong (it doesn’t need spaces between the classes as it’s the same element):

      (function($){ $(window).on("load",function(){ $(document).on("click",".menu.mobile_switch_on.show_sub_menu .menu-item ._mPS2id-h",function(){ $(".mobile-hamburger-toggle.is-active").trigger("touchstart"); }); }); })(jQuery);

      This should work

      Reply
      • Igor
        Posted on May 28, 2020 at 21:24 Permalink

        Unfortunately it didn’t work, did you mention that I also have to do something in my CSS? Could you explain it better?

        Reply
        • malihu
          Posted on May 29, 2020 at 00:23 Permalink

          No, you don’t have to add any CSS.

          The script does work, you just need to test it by loading your page on smaller screen/viewport.

          I think that you loaded the page on desktop and then emulated the device toolbar to see the mobile menu but this won’t work because your theme loads a different event depending on the width of the viewport the moment the page was loaded.

          If you load your page on a smaller viewport, you’ll see that it works.

          In any case, if you want to test it from any viewport, you can change the script to include the click event:

          (function($){ $(window).on("load",function(){ $(document).on("click",".menu.mobile_switch_on.show_sub_menu .menu-item ._mPS2id-h",function(){ $(".mobile-hamburger-toggle.is-active").trigger("touchstart").trigger("click"); }); }); })(jQuery);

          although triggering both touchstart and click events is just for testing, because a mobile user will always load the page on a mobile/smaller viewport (you can leave it as it is though if you want… doesn’t really matter).

          Hope this makes sense ๐Ÿ™‚

          Reply
  14. Dominik
    Posted on April 28, 2020 at 16:18 Permalink

    Hi, thanks for your awesome plugin!

    However, I don’t know anything about Php and your code doesn’t work. I hope you can help me!

    Thank you

    Reply
    • malihu
      Posted on April 30, 2020 at 02:22 Permalink

      Hello,

      Each site needs its own code. Change the code you added to this:

      (function($){ $(window).on("load",function(){ $(document).on("click",".main-navigation.toggled-on .menu-item ._mPS2id-h",function(){ $(".main-navigation.toggled-on .menu-toggle").trigger("click"); }); }); })(jQuery);

      Also, go to “Page scroll to id” settings and change the “Offset” from the 100 value you entered to:

      .navigation-top

      Save changes and test your page.

      Reply
      • Dominik
        Posted on April 30, 2020 at 11:11 Permalink

        Thank you very much! It worked!

        Reply
  15. Tiffany Ventura
    Posted on March 31, 2020 at 21:37 Permalink

    https://staging-hotelmillwright.kinsta.cloud/rooms/ we’re using the7 theme, I can’t quite get it to work for closing the mobile menu, Code I have so far:

    (function($){
    $(window).on(“load”,function(){
    $(document).on(“click”,”.dt-mobile-header”,function(){
    $(“.dt-close-mobile-menu-icon”).trigger(“click”);
    });
    });
    })(jQuery);

    Reply
    • malihu
      Posted on April 1, 2020 at 10:51 Permalink

      You don’t have “Page scroll to id” plugin activated in your site. This post/tutorial is about using “Page scroll to id” plugin.

      Reply
      • Tiffany Ventura
        Posted on April 1, 2020 at 18:12 Permalink

        Apologies, I deactivated it yesterday for my client to review. I have it activated now.

        Reply
        • malihu
          Posted on April 2, 2020 at 11:09 Permalink

          No problem. Try this script:

          <script> (function($){ $(window).on("load",function(){ $(document).on("click","#mobile-menu .menu-item ._mPS2id-h",function(){ $(".dt-close-mobile-menu-icon > span").trigger("click"); }); }); })(jQuery); </script>

          Reply
          • Tiffany Ventura
            Posted on April 2, 2020 at 17:58 Permalink

            Thank you! That worked ๐Ÿ™‚ I applaud your excellent support and plugin!!

  16. Xuan Yee
    Posted on March 7, 2020 at 13:19 Permalink

    Please provide me advice on how I can collapse the mobile menu after clicking on anchor links. Thank you!

    Reply
    • malihu
      Posted on March 7, 2020 at 15:37 Permalink

      Try this code:

      <script> (function($){ $(window).on("load",function(){ $(document).on("click",".offcanvas-sidebar .menu-item ._mPS2id-h",function(){ $(".site-overlay").trigger("click"); }); }); })(jQuery); </script>

      Reply
      • Xuan Yee
        Posted on July 18, 2020 at 20:08 Permalink

        Very sorry to bother you again and thank you for your help.

        I updated my theme and the code stopped working on mobile (still works on small-width desktop).

        (function($){
        $(window).on(“load”,function(){
        $(document).on(“click”,”#menu-default-menu .menu-item ._mPS2id-h”,function(){
        $(“.site-overlay”).trigger(“click”).trigger(“touchstart”);
        });
        });
        })(jQuery);

        I tried changing to the above and it still doesn’t work on mobile (but works on small-width desktop).

        Reply
        • Xuan Yee
          Posted on July 19, 2020 at 02:24 Permalink

          It’s working now – I think it was just cache. Nonetheless thanks you’re a hero!

          Reply
          • malihu
            Posted on July 19, 2020 at 16:18 Permalink

            Awesome! Glad I helped ๐Ÿ™‚

Post a comment

Your e-mail is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
You can write or copy/paste code directly in your comment using the <code> tag:
<code>code here...</code>
You may also use the data-lang attribute to determine the code language like so:
<code data-lang-html>, <code data-lang-css>, <code data-lang-js> and <code data-lang-php>

css.php