Correcting Web Accessibility Problems in Navigation Menus

The W3C provides detailed instructions for creating accessible menus which we have summarized below along with providing some code examples for improving the accessibility of your menus:

  1. Verify that the menu is marked up using list markup (ul and li).
  2. If a link to the current page is included in the menu, ensure that it includes the aria-current attribute and that CSS is used to visually identify the page as selected.
  3. Ensure links that open a submenu include a visual indication that they open a submenu and aria-expanded attributes.
  4. When activated the triggering links aria-expanded attribute must change to “true”, when deactivate it must change back to “false”.
  5. Ensure submenus do not open when tabbing to a link, if the triggering link is used to open a webpage in addition to opening a submenu a seperate button must be used to trigger the submenu.
  6. Ensure that the “enter” or “space” key can be used to open submenus without triggering a link to open.

Improving the Default WordPress Nav Menu

For many websites making your navigation menu accessible can be accomplished by using the latests Bootstrap Nav Walker.  Complete instructions are provided in the GitHub readme, linked above. If you need help, for a fee one of our accessibility expects can install the latests Bootstrap Nav Walker on your website.

Improving the Elementor Nav Menu Widget

Include the following code in your theme’s scripts file to improve the accessibilty of the Elementor Nav Menu Widget when your menu has only one level of dropdowns.

jQuery(document).ready(function($) {
// improve the accessibility of the elementor main menu
setTimeout(function(){
	jQuery(".sub-menu").attr('role','menu');
	jQuery("a.has-submenu").attr('aria-haspopup','true');
	jQuery(".elementor-nav-menu--dropdown .elementor-sub-item").removeAttr('tabindex');
	}, 500);

	$(document).on("click", "a.has-submenu", function(event) {
		event.preventDefault();
		jQuery(".elementor-nav-menu--dropdown .elementor-sub-item").removeAttr('tabindex');
	});

	$(document).on("keypress", "a.has-submenu", function(event) {
	event.preventDefault();
	var key = event.key;
	if(key === ' '){
	$( this ).trigger( "click" );
	}

	});

// // correct spacebar keypress triggering page slide
    jQuery("body").on("keypress", ".elementor-menu-toggle", function (event) {
        var code = event.key || event.code; //Get keycode.
        if (code != 'Tab' && code != ' ' && code != 'Enter' && code != 'ArrowLeft' && code != 'ArrowUp' && code != 'ArrowRight' && code != 'ArrowDown') //Check each key.
            return; //If it isn't any of those, don't worry about it.

        if ((code == ' ' || code == 'Enter' || code == 'ArrowLeft' || code == 'ArrowRight') && jQuery(':focus').is('input')) //If a space/enter or left/right arrow is pressed in the input, then allow it to happen as normal.
            return;

        event.preventDefault()

    });

	});

Include the following code in your theme’s scripts file to improve the accessibilty of the Elementor Nav Menu Widget when your menu is more 2 or more levels deep.

jQuery(document).ready(function($) {
  // improve the accessibility of the elementor main menu
  setTimeout(function(){
    jQuery(".sub-menu").attr('role','menu');
    jQuery("a.has-submenu").attr('aria-haspopup','true');
    jQuery(".elementor-nav-menu--dropdown .elementor-sub-item").removeAttr('tabindex');
    }, 500);

  $(document).on("click", "a.has-submenu", function(event) {
     event.preventDefault();
    });

      // disable opening on submenu on focus
  $(document).on("focus", "a.has-submenu:not(.elementor-sub-item)", function(event) {

    event.preventDefault();
    if($( this ).attr('aria-expanded') === 'true'){
    $( this ).trigger( "click" );
    }
    });

    // disable subitem links and add expanded notice
    $(document).on("click", "ul.sub-menu>li>a.has-submenu", function(event) {
      event.preventDefault();
       return false;
      });
      $(document).on("focus", "ul.sub-menu>li>a.has-submenu", function(event) {

        event.preventDefault();

       if($(this).find('.expandedNotice' ).length === 0){
        $( this ).append(' <span class="expandedNotice screen-reader-text">expanded menu</span> ');
       }

        if($( this ).attr('aria-expanded') === 'true'){
        }else{
          $('.expandedNotice').detach();
        }
         return false;
        });


    $(document).on("keypress", "a.has-submenu", function(event) {
    event.preventDefault();
    var key = event.key;
    if(key === ' '){
      if($( this ).attr('aria-expanded') === 'false'){
    $( this ).trigger( "click" );
      }
    }

    });

// correct elementor mobile menu not opening on keypress
jQuery("body").on("keypress", ".elementor-menu-toggle", function (event) {
	var code = event.key || event.code; //Get keycode.
	if (code != 'Tab' && code != ' ' && code != 'Enter' && code != 'ArrowLeft' && code != 'ArrowUp' && code != 'ArrowRight' && code != 'ArrowDown') //Check each key.
		return; //If it isn't any of those, don't worry about it.

	if ((code == ' ' || code == 'Enter' || code == 'ArrowLeft' || code == 'ArrowRight') && jQuery(':focus').is('input')) //If a space/enter or left/right arrow is pressed in the input, then allow it to happen as normal.
		return;

	event.preventDefault()

});

});

Generic Menu

The following example code can be used with some modification to make a navigation menu more accessible. If you need assistance, for a fee one of our accessibility expects can customize this code for your particular website.

jQuery(document).ready(function($) {

jQuery(".dropdown-menu").attr('role','menu');
jQuery("a.dropdown-toggle").attr('aria-haspopup','true');
jQuery("a.dropdown-toggle").append(' <span class="caret"></span>');

jQuery("a.dropdown-toggle").on("click keypress", function(event)

{
var thisid = jQuery(this).attr("id");

var key = event.key;

if(key === ' '){
event.preventDefault();
$( this ).trigger( "click" );
}else{

if(jQuery(this).attr('aria-expanded') === 'false') {
jQuery(this).attr('aria-expanded','true');
} else{
jQuery(this).attr('aria-expanded','false');
}

$("a.dropdown-toggle").each(function(){

if(jQuery(this).attr("id") !== thisid ){
jQuery(this).attr('aria-expanded','false');
}
});

}

});

// correct spacebar keypress triggering page slide
jQuery("body").on("keypress", "a.dropdown-toggle", function (event) {
var code = event.key || event.code; //Get keycode.
if (code != 'Tab' && code != ' ' && code != 'Enter' && code != 'ArrowLeft' && code != 'ArrowUp' && code != 'ArrowRight' && code != 'ArrowDown') //Check each key.
    return; //If it isn't any of those, don't worry about it.

if ((code == ' ' || code == 'Enter' || code == 'ArrowLeft' || code == 'ArrowRight') && jQuery(':focus').is('input')) //If a space/enter or left/right arrow is pressed in the input, then allow it to happen as normal.
    return;

event.preventDefault()

});
});

Similar Posts

  • a11y stands for ACCESSIBILITY

    The A11Y Project is a community-driven effort to make digital accessibility easier. The A11Y Project website provides: Fundamentals and principles behind accessible design A checklist based on the Web Content Accessibility Guidelines (WCAG) A community of designers who are interested in improving Web Content Accessibility Many resources, including: tools, books, videos, podcasts, newsletters, even professional help!…

  • Did you know? All Headings are not Equal!

    A very common accessibility problem is the lack of properly formatted Heading tags in a webpage (H1, H2, H3, H4, H5 or H6). Sometimes headings are not present or they may be incorrectly nested (ie… H2 before H1, H3 before H2 etc…). Headings communicate the organization of the content on the page and can be…

  • Did you know that most online forms are not completely accessible to screen reader users?

    Forms must be carefully constructed to ensure accessibility. The form element must include an aria-label describing the purpose of the form. In the event of error or during a multiple step entry process, information previously entered must be auto populated or available for the user to select. Each form field must be labeled using the…

  • How to Build an Inclusive Company Website

    Image via Pexels A website is a crucial element of any successful company. Your website should introduce potential customers to your business and show them what you have to offer. Unfortunately, many websites are not inclusive and don’t necessarily cater to people with disabilities. If you want to draw in a wider audience and make…

  • |

    Department of Justice (DOJ) Web Accessibility Mandates

    The Department of Justice (DOJ) mandates that state and local government (Title II) websites and mobile apps comply with WCAG 2.1 Level AA standards, with deadlines of April 24, 2027, for populations of 50,000+ and April 26, 2028, for smaller entities. This covers all digital services, including forms, portals, and third-party tools. The rule covers…

  • Web Accessibility Training

    In this web accessibility training course you will learn web and digital accessibility skills for web, documents, and mobile content. This self-paced course cover the most important aspects of Web Accessibility and will teach you how people with disabilities use the web and different types of assistive technologies. Course Length Approximately 8 hours of reading…

Accessibility Tools
hide