HTML for Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The for attribute connects UI text to related controls. On a <label>, its value is the id of the form control that label describes. On an <output>, its value is a space-separated list of ids for elements that feed the result.

Explicit labeling improves accessibility (screen readers announce the name) and usability (clicking the label focuses or toggles the control). You can also wrap a control inside <label> for an implicit association without for.

Baseline · Widely available.

Not deprecated and not experimental. Stable label association across modern browsers. Applies to <label> and <output> only.

JavaScript: use htmlFor.

for is a reserved word in JavaScript. Read and write the attribute with labelElement.htmlFor.

HTML Syntax

syntax.html
<label for=" element_id">

On <output>, list one or more ids: for="id1 id2".

Attribute Values

Meaning depends on the element that carries for:

ElementValueTry it
<label>One id of a labelable control (input, select, textarea, and more)Try it
<label> (checkbox / radio)Same rule — clicking the label toggles or selects the inputTry it
<output>Space-separated list of ids that contribute to the calculationTry it

Live Preview

Change which control the label points to, then click the label text. Focus should move to the matching field.

A real <label for=…>. Apply a target id, then click the label.

Labeled controls

I agree

for="live-first"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Explicit Label and Input

Match for on the label with id on the control.

HTML
<label for="username">Username:</label>
<input type="text" id="username" name="username">
Try It Yourself

How It Works

  1. Give the control an id. id="username" must be unique in the document.
  2. Point the label. for="username" must match that id exactly.
  3. Click the label. The browser focuses the linked input.
  4. Screen readers benefit. The accessible name comes from the label text.

Example 2 — Checkbox Labels

Labels enlarge the tap target — ideal for small checkboxes.

HTML
<p>
  <label for="cheese">I like cheese.</label>
  <input type="checkbox" id="cheese" name="cheese">
</p>
<p>
  <label for="peas">I like peas.</label>
  <input type="checkbox" id="peas" name="peas">
</p>
Try It Yourself

How It Works

  1. Same for / id rule. Each checkbox has its own unique id.
  2. Click the text. The browser toggles the linked checkbox.
  3. Larger hit area. Users (especially on touch) do not need to hit the tiny box.
  4. One label per preference. Multiple labels may share one id if you need several captions for the same control.

Example 3 — output with Multiple Ids

On <output>, for lists the inputs that contribute to the result.

HTML
<input type="range" id="b" name="b" value="50"> +
<input type="number" id="a" name="a" value="10"> =
<output id="result" name="result" for="a b">60</output>

<script>
  const a = document.getElementById("a");
  const b = document.getElementById("b");
  const result = document.getElementById("result");
  function update() {
    result.value = a.valueAsNumber + b.valueAsNumber;
  }
  a.addEventListener("input", update);
  b.addEventListener("input", update);
</script>
Try It Yourself

How It Works

  1. Name the sources. for="a b" lists the ids that feed the calculation.
  2. Space-separated. Unlike label, output may reference several elements.
  3. Script updates the value. for documents the relationship; JavaScript still computes the number.
  4. Accessibility hint. Assistive tech can treat the result as related to those inputs.

Edge Cases & Pitfalls

Most bugs are mismatched ids or putting for on the wrong element.

match

Ids must match exactly

Typos, wrong case, or a missing control mean the label does nothing useful. Keep ids unique in the whole document.

scope

Only on label and output

Do not put for on <input>. Controls use id; labels and outputs use for.

labelable

Target must be labelable

If for points at a non-labelable element (or input type="hidden"), the association has no effect.

a11y

Avoid interactive content inside labels

Links or nested buttons inside a <label> make activation confusing. Keep terms links outside the label when possible.

script

Use htmlFor in JavaScript

element.for is not the HTML attribute API. Set label.htmlFor = "id" instead.

Browser Support

for is an HTML attribute for label and output in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

for

Label association with for is universal in modern and legacy browsers. The output for attribute is widely supported in current Chrome, Firefox, Safari, and Edge.

100% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer label for: yes
Supported
for Widely supported

Bottom line: Safe to rely on for label–control association everywhere. Prefer explicit for + id when layout keeps the label and control apart.

Key Takeaways

  • Role: on label, point to one control id; on output, list related ids.
  • Elements: only <label> and <output>.
  • Alternative: wrap the control in <label> for an implicit link (no for required).
  • Script: use htmlFor, not for.

One line: for links a label (or output) to control ids — match carefully and keep ids unique.

Frequently Asked Questions

On a label, for holds the id of the form control that label describes. Clicking the label focuses or activates that control. On an output element, for is a space-separated list of ids of elements that contribute to the result.
label and output. Labels are the common case for form fields. Controls themselves use id; they do not take for.
A single id that matches one labelable control in the same document — for example an input (not type=hidden), select, textarea, button, meter, progress, or output.
No. Wrapping the control inside the label creates an implicit association without for. Explicit for + id is still preferred when the label and control are separate in the layout, and for assistive-tech compatibility.
Use labelElement.htmlFor because for is a reserved word in JavaScript. Reading or writing htmlFor updates the for attribute.
It is Baseline widely available. Label association with for has worked in major browsers for decades. The output for attribute is also widely supported in modern browsers.

Next: form

Link a control to a form anywhere in the page using the form attribute.

form tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful