Stop russian invasion of Ukraine 🇺🇦

Closing modal window on the ESC button

Closing modal window on the ESC button

It’s time to improve the accessibility of modal dialogs too! Add an event listener that waits when the ESC button is pressed and then runs the same action as the .modal__closeclass:


// close CTA section on the ESC button
document.body.addEventListener('keydown', function (e) {
  if (e.key === 'Escape') {
    document.querySelector('.modal__close').click();
  }
});

Try it live on this example

It might be interesting for you

Let’s create together

Start a project

Let’s create together

What are your interested in?

Budget

Timeframe

Personal details

Thank you for getting in touch!
We appreciate you contacting us.
We will get back to you shortly.
Dumka
Something went wrong while submitting the form.
Close