⚠️ This lesson is retired and might contain outdated information.

Collapse a Submenu on the blur Event to Improve Navigation Accessibility

Lindsey Kopacz
InstructorLindsey Kopacz
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 2 years ago

In this lesson, we extend off Expand a Menu on a focus Event to Improve Navigation Accessibility to improve the navigation. We use a bit of JavaScript to collapse the submenu on blur. This is important to ensure that if someone cannot use a mouse, they are still able to close the menu.

https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event

Instructor: [00:00] When we tab to the submenu links, notice that it does not close. What we have to do is make sure that when we tab away from this last submenu item, we remove the focus class. Let's explore this a little bit more through code.

[00:18] We're going to do const submenus equals document.querySelector, all, and then submenu. Let's just console.log that, just to make sure that we have that. Perfect. Even though we only have one submenu here I'm still going to use forEach here, because if there's multiple, we want to make sure that we do the same thing for each of them.

[00:50] submenus, for each submenu. Now, let's just console.log submenu, just to make sure. All right, great. What I want to do here is I want to get all of the submenu links. I'm going to take const submenu links equals submenu, querySelector, all, submenu link.

[01:30] One last thing I'm going to do is I am going to actually turn this into an array, because I want to use the pop method. If I console.log submenu links, this is what we get. Now, I'm going to get the last menu item by popping the last item off this array.

[01:56] Now, let's refresh. Great, we have that last menu link. I'm just going to call this a variable, const lastSubmenuLink. We have all of our variables set up. What we want to do here is the last submenu link, we want to add the blur event.

[02:21] We're going to take the submenu, and we are going to grab the parent element. Great, so this item right here is where we want to remove the focus class. Let's see what happens. Great, it works.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today