# Close the Mobile Menu After Tapping an On-Page Link

> Closes the Navigation block's full-screen mobile menu when a visitor taps a link to a section of the same page (like #pricing), so they actually see where they jumped to.

- Type: JavaScript
- Runs: Site-wide footer
- Category: [Frontend & Design](https://wpide.com/library/category/frontend/)
- Tags: navigation, mobile-menu, one-page
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/frontend-close-mobile-nav-on-anchor/

## Code

```javascript
document.addEventListener('click', function (e) {
    var link = e.target.closest ? e.target.closest('.wp-block-navigation__responsive-container.is-menu-open a[href*="#"]') : null;
    if (!link || link.pathname !== window.location.pathname || !link.hash) { return; }
    var container = link.closest('.wp-block-navigation__responsive-container');
    var close = container && container.querySelector('.wp-block-navigation__responsive-container-close');
    if (close) { close.click(); }
});
```

## Notes

For block themes using the Navigation block. Links to other pages work as before.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Close the Mobile Menu After Tapping an On-Page Link" in its snippet library and add it in one click; or create a new JavaScript snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
