Skip to content

Instantly share code, notes, and snippets.

@brettkelly
Last active April 8, 2025 20:29
Show Gist options
  • Save brettkelly/3031db423d3750c1636fb686812b3cf8 to your computer and use it in GitHub Desktop.
Save brettkelly/3031db423d3750c1636fb686812b3cf8 to your computer and use it in GitHub Desktop.
Elementor JS fix for flyout menus that don't close on click
<script>
function handlePopupAnchorClick(e) {
const currentUrl = window.location.href.split('#')[0];
const clickedUrl = this.href.split('#')[0];
// If we're on the same page
if (currentUrl === clickedUrl) {
e.preventDefault();
const hash = this.href.split('#')[1];
if (hash) {
const element = document.getElementById(hash);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
}
}
const popup = jQuery(this).closest('.elementor-popup-modal');
if (popup.length) {
const popupId = popup.attr('data-elementor-id');
const popupInstance = elementorFrontend.documentsManager.documents[popupId];
if (popupInstance) {
popupInstance.getModal().hide();
} else {
try {
const elementorPopup = elementorFrontend.documentsManager.documents[popupId];
jQuery(popup).find('.dialog-close-button').trigger('click');
} catch(e) {
console.error('Error:', e);
}
}
} else {
console.log('popup.length is 0');
console.log('Parent elements:', jQuery(this).parents().map(function() {
return this.className;
}).get());
}
}
jQuery(window).on('elementor/frontend/init', function() {
jQuery('body').on(
'click', // event
'.elementor-location-popup .menu-item a, .elementor-location-popup a.elementor-button', // selector(s)
handlePopupAnchorClick // callback
);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment