Building an Accessible Popup using jQuery

In this tutorial, we will guide you through the process of building accessible popups using the power of jQuery. We will focus on implementing keyboard accessibility, managing focus, leveraging ARIA attributes, and following best practices to ensure that your popups are usable and inclusive.

So, let’s dive in and learn how to create accessible popups that enhance user experience and make your website more inclusive!”

Setting Up the HTML Structure

To start building our accessible popup, we need to set up the HTML structure. We’ll define the markup for the popup container and add trigger elements that will open the popup when interacted with.

HTML

Attributes used and their purpose:

  1. aria-haspopup="dialog": This attribute is applied to the trigger button (modalTrigger). It indicates that activating the button will display a dialog or a modal popup.
  2. aria-expanded="false": This attribute is also applied to the trigger button (modalTrigger). It indicates the current state of the modal, where “false” means that the modal is initially hidden or collapsed.
  3. role="dialog": This attribute is applied to the modal container (modalContainer). It specifies the role of the element as a dialog or a modal window.
  4. aria-modal="true": This attribute is also applied to the modal container (modalContainer). It indicates that the modal is a top-level window and should be treated as a modal dialog by assistive technologies.
  5. aria-labelledby="modalTitle": This attribute is applied to the modal container (modalContainer). It references the element with the ID “modalTitle” (<h2 id="modalTitle">) to provide an accessible name or label for the modal.
  6. aria-describedby="modalDescription": This attribute is also applied to the modal container (modalContainer). It references the element with the ID “modalDescription” (<p id="modalDescription">) to provide a description or additional information about the modal.
  7. aria-label="Close Modal": This attribute is applied to the close button (modalClose). It provides an accessible name or label for the button, specifying that it is used to close the modal.

These ARIA attributes help improve the accessibility of the modal by providing additional information and context for assistive technologies, such as screen readers, to understand the purpose and behavior of the modal. They ensure that users with disabilities can navigate and interact with the modal effectively.

It’s important to use ARIA attributes appropriately and according to their intended purpose to ensure the accessibility of your web content.

Popup Functionality using jQuery

JavaScript

The code provided sets up an accessible popup using jQuery. Here’s a description of how the code works:

  1. The code is wrapped within a $(document).ready() function to ensure that the code executes after the document has finished loading.
  2. The variables modalTrigger and modal are assigned jQuery objects representing the trigger button and the modal container, respectively.
  3. The showPopup() function is defined to handle the opening of the modal. When the function is called, it adds the “show” class to the modal container and sets the aria-hidden attribute to “false” to indicate that the modal is visible.
  4. Inside the showPopup() function, the first and last focusable elements within the modal are identified using the focusableElements selector. The first element is stored in the firstFocusableElement variable, and the last element is stored in the lastFocusableElement variable.
  5. The keydown event listener is attached to the modal using the modal.on("keydown", trapFocus) syntax. This event listener calls the trapFocus() function, which manages the keyboard focus trapping within the modal.
  6. The trapFocus() function checks for key events. If the “Tab” key is pressed, it traps the focus within the modal by checking the current active element and shifting the focus to the first or last focusable element accordingly. If the “Escape” key is pressed, it calls the closeModal() function to close the modal.
  7. The closeModal() function is defined to handle the closing of the modal. It removes the “show” class from the modal container, sets the aria-hidden attribute to “true”, removes the keydown event listener, and sets the focus back to the modal trigger button. It also appends a visually hidden ARIA alert to announce the closure of the modal to screen readers.
  8. The modalTrigger is assigned a “click” event listener that triggers the showPopup() function when clicked. Similarly, the “click” event listener is attached to the element with the ID “modalClose” to call the closeModal() function when clicked.

Style the popup

Finally some basic styling of our popup.

CSS

Leave a Comment

Your email address will not be published. Required fields are marked *