Skip to content
OSULZER.AT
Code1 min read· updated September 3, 2024

Elementor Mega Menu: Open the Submenu on Focus

Elementor mega menus only open with Enter or the space bar when you navigate by keyboard. This short JavaScript snippet opens the submenus on focus.

The following code lets you open Elementor’s new mega menus directly on focus. Without this code, the submenus behave so that you first have to press the Enter key or the space bar when navigating with the keyboard. Since there is no suitable ARIA label, this unfortunately makes little sense, and in my opinion the behavior is not ideal to begin with.

document.addEventListener('DOMContentLoaded', function() {
    // Select all buttons inside the element with class 'e-n-menu-heading'
    document.querySelectorAll('.e-n-menu-heading button').forEach(function(button) {
        // Add a focus event listener to each button
        button.addEventListener('focus', function() {
			
                let enterEvent = new KeyboardEvent('keydown', {
                    key: 'Enter',
                    keyCode: 13,
                    code: 'Enter',
                    which: 13,
                    bubbles: true, // This ensures the event bubbles up through the DOM
                });
			
                button.dispatchEvent(enterEvent);
        });
    });
});

PHP Freelancer

Prefer to have it done for you?

Customizations, plugins, and integrations - cleanly implemented, €120/h plus VAT.

See the PHP freelancer

New project?

Contact.

Tell me briefly about your project - I'll get back to you personally.

Your data is used solely to handle your inquiry. More in the privacy policy.