I came across an issue with an old wordpress site where the mobile menu did not close when a menu item link to internal page content because the url started with # hash.
So I worked out that you could close the menu suing jQuery by doing the following.
$('.mobile-button.active').click();
So this was the same as the user clicking the burger menu to close it. But then I needed to only do this when a link was clicked that started with # hash. The following was used.
jQuery(document).ready(function($) { $('a[href^="#"]').on('click', function(e) { // Prevent the default behavior of the link //e.preventDefault(); // Get the href attribute of the clicked link var href = $(this).attr('href'); // Check if the URL starts with '#' if (href.charAt(0) === '#') { // URL starts with '#', do something console.log('URL starting with "#" clicked'); $('.mobile-button.active').click(); //go ahead and close the menu } }); });