Name Controls From Visible Text

In the last lesson, you read names in the Accessibility pane. The browser takes an element’s name from aria-labelledby first, then aria-label, then its own label (a <label>, an alt, a <legend>), and for links and buttons, then their content. The icon-only Menu button got its name from aria-label, because it has no words of its own. This lesson moves from reading names to choosing them.

In this lesson, you’ll give the home page’s controls names that match what’s on screen, name the footer’s icon-only links, and tie a hint to a field on the sign-up form. Start with the newsletter’s Join button, whose name nobody has questioned yet.

Make the Name Match the Visible Text

The home page has two controls that say “Join”. The nav’s Join link goes to the page about joining the choir, and the button at the bottom of the Newsletter section signs someone up for the monthly email. Someone who hears only “Join” can’t tell which is which.

A volunteer has already given the starter’s button an aria-label. Delete it in index.html to start from what Marjorie had:

Language: HTML
      <form class="newsletter" action="thanks.html">
        <label>Email <input type="email" name="email" autocomplete="email"></label>
        <button class="button">Join</button>
      </form>

How would you give Tomás a fuller name and keep the short word on screen? Most people use aria-label, as on the Menu button:

Language: HTML
        <button class="button" aria-label="Sign up for the newsletter">Join</button>

Save, but before you inspect it, decide what name you expect. Does Tomás get “Join” plus the longer label, or something else?

Right-click the button, choose Inspect, and open the Accessibility tab in the Elements panel’s sidebar:

The Accessibility pane for the Join button shows the name "Sign up for the newsletter" from aria-label, with Contents "Join" crossed out below it.
The aria-label Replaces the Button's Text

The name is “Sign up for the newsletter”, from aria-label. The pane still lists the button’s contents, “Join”, but crossed out, because the browser found the text and didn’t use it. An aria-label doesn’t add to an element’s text. It replaces it as the name, so “Join” isn’t in the name at all.

Why does a missing word matter if the longer name is clearer? Think of someone who controls their computer by voice, such as with Voice Control on a Mac. They look at the screen, see “Join”, and say “Click Join”. The software matches what they say against the names of the controls, and this button’s name doesn’t contain “Join”, so the command may not reach it.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.