Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

output — a calculation or user-action result

The `output` element represents the result of an application calculation or a user action. Its `for` attribute identifies elements that influence the result, but does not perform calculations.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<output for="input-id-1 input-id-2" name="result">initial result</output>

output presents the result of an application calculation or a user action. Use samp instead when quoting output from a previously run program. Both output tags are required; its content may include text and other phrasing content.

The for attribute contains unique, space-separated element IDs from the same tree. It describes a relationship, but does not trigger calculations. form explicitly associates the result with a form, and name gives it a name. An output value is not submitted with the form, even when it has a name.

In JavaScript, value reads descendant text; assigning it replaces the content with text and preserves the default value for resetting. defaultValue exposes the default restored by a form reset. Put the initial result in the element's content, not in a value attribute.

In the example, the input event handler adds numeric values. The initial result is 5; with A set to 4 and B set to 3, it becomes 7. The Reset button restores the result to 5 and the sliders to their initial values.

Examples

<!doctype html>
<meta charset="utf-8">
<title>output</title>
<form id="calculator">
  <label for="a">A</label>
  <input id="a" type="range" min="0" max="10" value="2">
  <label for="b">B</label>
  <input id="b" type="range" min="0" max="10" value="3">
  <label for="result">A + B</label>
  <output id="result" name="sum" for="a b">5</output>
  <button type="reset">Reset</button>
</form>
<script>
  const calculator = document.getElementById('calculator');
  const a = document.getElementById('a');
  const b = document.getElementById('b');
  const result = document.getElementById('result');

  calculator.addEventListener('input', () => {
    result.value = String(a.valueAsNumber + b.valueAsNumber);
  });
</script>

Result

Initial: 5
A = 4, B = 3: 7
After Reset: 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources