Skip to main content

How to Connect a label to a Form Field with the for Attribute in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

<label for="email"> connects a label to the form field whose id is email, wherever the two are on the page. Clicking the label then puts the cursor in the field, and screen readers announce the field by the label's text.

Syntax​

Syntax
<label for="<id>"><text></label>
<input id="<id>">
PartWhat it does
for="<id>"The id of the field this label belongs to. It must match the id exactly, capital letters included.
<text>The label's text.
id="<id>"The field's id, the same value as in for.

MDN's label page calls this an explicit association, and MDN's for attribute page describes the attribute.

The Example​

The example form has the label Email address and a text box whose id is email, connected by for="email".

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding a label Email address with for email, and a text box with the id email and the name email.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Label for</title>
</head>
<body>
<form>
<label for="email">Email address</label>
<input id="email" name="email">
</form>
</body>
</html>

Step 2: Click the Label​

Double-click index.html to open it in your browser. Click the words Email address: the cursor jumps into the text box, so you can type straight away. Type ana@example.com:

The page in Chromium: the label Email address and a focused text box with ana@example.com typed in it.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Clicking the label focused the text box: Chromium reported the element with the id email as the focused one.
  • The label's text became the field's name. In Chromium's accessibility tree, the information screen readers use, the text box was named Email address.

Common Mistakes​

MistakeWhat happensDo this instead
A for that differs from the id in capital letters, such as for="Email" for id="email"Nothing is connected: clicking the label left the focus on the page, and the text box had no name in the accessibility treeMake for exactly the same as the id
Putting the field's name in forfor only looks at id: with for="mail" and a text box with id="email" and name="mail", clicking the label did nothingUse the id in for

Troubleshooting​

SymptomLikely causeFix
Clicking the label doesn't put the cursor in the fieldfor and id don't matchCopy the id into for
A screen reader doesn't announce the field's labelThe label isn't connectedMatch for to the field's id

FAQ​

Do I need for when the field is inside the label?​

No. A field inside its label is connected already. for is for a label that sits somewhere else on the page.

How do I read for in JavaScript?​

As htmlFor, because for is a reserved word in JavaScript. For the example's label, document.querySelector('label').htmlFor was 'email'.

Can the label come after the field?​

Yes. for connects them wherever they are on the page: with the label after the text box, clicking it still put the cursor in the box, and the box was still named Email address.

Conclusion​

for on a label and id on a field, with exactly the same value, connect the two: the label becomes clickable, and it names the field for screen readers. If clicking the label does nothing, compare the two values letter by letter.

Additional Resources​