Output For Form Calculation Result

Display live calculation results using the output element bound to form inputs with the for attribute.

HtmlForm Validation

Code

htmlsnippet
8 lines
<form oninput="result.value = (a.valueAsNumber || 0) + (b.valueAsNumber || 0)">
  <fieldset>
    <legend>Addition Calculator</legend>
    <label>A: <input type="number" name="a" value="0" /></label>
    <label>B: <input type="number" name="b" value="0" /></label>
    <label>Result: <output name="result" for="a b">0</output></label>
  </fieldset>
</form>

How the snippet works

<output> semantically marks a computed result, which screen readers announce as a live region.

The form collects input via type="number" field, each tied to a <label> so every control announces what it is.

Statements that matter

<form oninput="result.value = (a.valueAsNumber || 0) + (b.valueAsNumber || 0)">
<legend>Addition Calculator</legend>
<label>A: <input type="number" name="a" value="0" /></label>

Interactive Tools for This Topic

FAQ

Which non-generic elements does the markup use?
`<form>`, `<fieldset>`, `<legend>`, `<label>`, `<input>`, `<output>` — each carries native semantics (or a browser behavior) that generic containers do not.