---
url: "https://web.dev/learn/forms/javascript"
titulek: "JavaScript | web.dev"
h1: "JavaScript Stay organized with collections Save and categorize content based on your preferences."
popis: "Find out how to use JavaScript to enhance your forms."
jazyk: "en"
kanonicka: "https://web.dev/learn/forms/javascript"
drobecky: ["web.dev", "Resources", "Forms"]
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-13 23:12:06"
znaku: 5678
slov: 737
kodovani: "iso-8859-2"
sha256: "acec23da061793e5535f21029a70f3941179a76a9b32ce29fd7b514f6a00a2ca"
---
JavaScript
Stay organized with collections
Save and categorize content based on your preferences.
Respond to form events You can use JavaScript to respond to user interactions on your form, reveal additional form fields, submit a form, and much more. Help users fill in additional form controls Imagine that you built a survey form. After a user selects one option,
you want to show an additional `` to ask a specific question related to the selection.
How can you only show the relevant `` element? You can use JavaScript to reveal an `` only when the associated `` is currently selected. `if (event.target.checked) { // show additional field } else { // hide additional field }` Let's look at the
[JavaScript code](https://codepen.io/web-dot-dev/pen/8e1e7a38790c75c267a978efa1d8e937?editors=0010) for the demo.
Have you noticed the `aria-controls`, and `aria-expanded` attributes?
Use these
[ARIA attributes](https://developer.mozilla.org/docs/Web/Accessibility/ARIA)
to help screen reader users understand when an additional form control is shown or hidden. Ensure users can submit a form without leaving a page Imagine you have a comment form. When a reader adds a comment and submits the form,
it would be ideal if they could immediately see the comment without a page refresh. To achieve this, listen to the `onsubmit` event, then use `event.preventDefault()` to prevent the default behavior,
and send the `FormData` using the [Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API).
Browser Support
42
14
39
10.1
[Source](https://developer.mozilla.org/docs/Web/API/Window/fetch) Your backend script can check if a `POST` request appears to be from the browser
(using the `action` attribute of a form element, where `method="post"`) or from JavaScript,
such as a `fetch()` request. `if (req.xhr || req.headers.accept.indexOf('json') !== -1) { // return JSON } else { // return HTML }` Always notify screen reader users about dynamic content changes.
Add an element with the `aria-live="polite"` attribute to your HTML,
and update the content of the element after a change.
For example, update the text to 'Your comment was successfully posted', after a user submits a comment. Learn more about [ARIA live regions](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/ARIA_Live_Regions). Validation with JavaScript Ensure error messages align with your site style and tone The wording of default error messages differs between browsers.
How can you make sure the same message is shown to all users,
and that the message aligns with your site's [style and tone](https://developers.google.com/style/tone)?
Use the [`setCustomValidity()`](https://developer.mozilla.org/docs/Web/API/HTMLInputElement/setCustomValidity)
method of the [Constraint Validation API](https://developer.mozilla.org/docs/Web/API/Constraint_validation)
to define your own error messages. Ensure users are notified about errors in real time The built-in HTML features for form validation are great for notifying users
about invalid form fields before the data is sent to your backend.
Wouldn't it be great to notify users as soon as they leave a form field? Listen for the
[`blur`](https://developer.mozilla.org/docs/Web/API/Element/blur_event)
event which fires when an element loses focus, and use the
[`ValidityState`](https://developer.mozilla.org/docs/Web/API/ValidityState) interface to detect if a form control is invalid. Ensure users can see the password they entered The text entered for `` is obscured by default,
to respect the privacy of users.
Help users to enter their password, by showing a `