HTML Output Element: Display Calculation Results
Represent a result
Use output for the result of a calculation or user action. Unlike an input field, it does not collect editable text. The for attribute can identify one or more source control IDs separated by spaces.
<label> Quantity <input id="qty" type="number" value="3"></label><label> Estimated total <output for="qty">15</output></label>15 is an initial result, not an automatic calculation.
Display a live calculation
HTML associates the result with its inputs, but it does not perform arithmetic. The small JavaScript example reads the number, multiplies it by a fixed price, and assigns the result to output. An event listener runs the calculation on edits. Treat this script as a preview of JavaScript concepts, not a new HTML rule.
<label> Quantity <input id="quantity" type="number" min="1" value="2"></label><label> Total <output id="total" for="quantity">10</output></label><script> const quantity = document.getElementById("quantity"); const total = document.getElementById("total"); quantity.addEventListener("input", () => { total.value = quantity.checkValidity() && quantity.value !== "" ? String(quantity.valueAsNumber * 5) : "Enter a valid quantity"; });</script>Changing the quantity to 4 displays 20. An empty or invalid quantity shows guidance.
Output is not submitted
Even if output has a name, it does not contribute a normal form-data entry. The server should recalculate authoritative totals from trusted prices and submitted quantities. An output element can be associated with a form without being a successful submission control.
<form> <label> Quantity <input name="qty" type="number" value="2"> </label> <label> Total <output name="total">10</output> </label></form>Form data contains qty=2, not total=10.
Common mistakes and practice
for is an association, not a formula. Give the result a meaningful label and avoid overwhelming users with rapid announcements. Test how updates are exposed by assistive technologies. Never trust a client-calculated amount as the final server price.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Output Element: Display Calculation Results — Knowledge Check
Apply the control concepts taught in this lesson.
What does the output for attribute contain?
Does output calculate the sum automatically?
Is a named output automatically included in form data?
Which property can JavaScript update to display a new calculation result?
Why associate an output with the controls used in its calculation?