How can I read a form field's current value as someone types?
Quick answer
Add an input event listener to the text field, then read the field’s current .value inside the handler and use it to update a preview. For text controls, input fires as the user edits, unlike change, which fires when the value is committed, such as when the field loses focus. [S1] Assigning a value in code does not itself fire input. [S1]
The question, the details, the next step.
A practical checklist
Give the text field and preview distinct IDs, and make the selectors match them.
Attach an
inputlistener to the field whose edits should drive the update.Read the field’s current
.valueinside the handler.Update the preview with the result you want to display.
Call the update function directly when your own code sets a value and needs to refresh the preview.
A few more useful answers
Why use `input` instead of `change` for a live preview?
For text controls, input fires as the user edits, while change fires when the value is committed, such as when the field loses focus. [S1]
Will changing `.value` in JavaScript trigger the listener?
No. Assigning a value in code does not itself fire input; call your update function directly when your code needs to refresh the preview. [S1]
Does the same behavior apply to checkbox and radio controls?
The documentation flags historical compatibility issues for checkbox and radio controls. [S1] The example here is for a text field, so do not assume it covers those other controls.
Sources
References used for this guide.
- [S1] Element: input event — Web APIs | MDN developer.mozilla.org · Checked
