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);
});
});
});




