Forms without a reload
Submit forms with fetch and render field errors.
What you will be able to do
- Use event.preventDefault().
- Use FormData for multipart submissions.
- Render field errors next to inputs.
- Re-enable the submit button in finally.
In plain English
Intercept submit, build FormData, send it and render the server response. Keep native validation as a convenience, but always send the data to the server for real validation.
form.addEventListener('submit', async (event) => {
event.preventDefault()
const button = form.querySelector('button')
button.disabled = true
try {
const response = await fetch('/api/signup', { method: 'POST', body: new FormData(form) })
if (!response.ok) throw new Error('Save failed')
} finally {
button.disabled = false
}
})Your turn
EasyPrevent the reload
Stop the default submit event.
JAVASCRIPT · starter
form.addEventListener('submit', (event) => {Check your understanding before moving on.
Knowledge checkQuestion 1
Which API builds a multipart body from a form?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.