HTML Output Element
In this page:
<form oninput="result.value = expression">
<output name="result" for="input1 input2"></output>
</form>
What Is the <output> Element?
The <output> element is a form element used to display the result of a calculation, such as a total, a sum, or any value derived from other inputs. It behaves like other form controls and is included when the form is submitted.
- Use <output> instead of a generic element whenever the displayed text is a calculated result tied to form inputs.
- Accepted attributes:
- for — space-separated ids of input elements
- form — id of associated form
- name — key for form.elements
Example: What Is the <output> Element?
<output>42</output>
Linking Output to Form Inputs with 'for'
The for attribute on <output> accepts a space-separated list of input IDs, explicitly declaring which fields the displayed result depends on. This strengthens the relationship for assistive technology and for developers reading the markup.
- List every input ID that contributes to the calculation inside the for attribute, even if there are several of them.
- Accepted values:
- for — space-separated list of element ids that contribute to the result
Example: Linking Output to Form Inputs with 'for'
<input type="number" id="a" value="2">
<input type="number" id="b" value="3">
<output for="a b">5</output>
Calculating Values with JavaScript
Because <output> does not calculate anything by itself, JavaScript is used to listen for changes on the related inputs and update the output's value property with the computed result.
Example: Calculating Values with JavaScript
<input type="number" id="a" oninput="document.getElementById('result').value = Number(a.value) + Number(b.value)">
<input type="number" id="b">
<output id="result"></output>
Styling the Output Element
Since <output> is an inline form element, it can be styled with ordinary CSS just like a span or input, letting you visually emphasize calculated results without changing how the element behaves.
Example: Styling the Output Element
<output style="font-weight: bold; color: green;">100</output>
Accessibility and the Output Element
The <output> element includes an implicit ARIA live region role in many browsers, meaning screen readers can automatically announce changes to its value, which is especially useful for calculated totals that update without a page reload.
Example: Accessibility and the Output Element
<label for="total">Total:</label>
<output id="total">0</output>
- Using a plain <span> or <div> to display a calculated result instead of the semantic <output> element.
- Forgetting to update the output's value with JavaScript whenever the linked inputs change.
- Leaving out the for attribute, which weakens the connection between the output and the inputs it is calculated from.
- The <output> element displays the result of a calculation or user action inside a form.
- Its for attribute lists the IDs of the input elements the result depends on.
- Output values must be updated manually with JavaScript whenever the related inputs change.
The <output> element is supported by all modern browsers.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep