HTML Output Element
In this page:
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.
Note: Use <output> instead of a generic element whenever the displayed text is a calculated result tied to form inputs.
Warning: An <output> element does not calculate anything on its own; its value must be set explicitly with JavaScript.
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.
Note: List every input ID that contributes to the calculation inside the for attribute, even if there are several of them.
Warning: The for attribute alone does not perform any calculation automatically; it only documents the relationship.
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.
Note: Use the form's oninput event to recalculate values immediately as visitors type or move a slider.
Warning: Forgetting to convert input values with Number() before adding them can concatenate strings instead of performing arithmetic.
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.
Note: Style the output distinctly from surrounding text so visitors can immediately recognize it as a calculated result.
Warning: Overly small or low-contrast styling on the output can make important calculated totals easy to miss.
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.
Note: Pair <output> with a clear <label> or descriptive text so its purpose is understood even before its value changes.
Warning: Relying solely on visual styling to signal an updated value excludes visitors using screen readers, who depend on the announced change instead.
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