How to Stop a Form from Reloading the Page with preventDefault in JavaScript
When a form is sent, the browser loads a new page by default, which wipes out anything your script showed. To handle the form in JavaScript instead, listen for the form's submit event and call event.preventDefault() first: the browser then skips its own sending, and the page stays as it is.
Syntax
function <handler>(event) {
event.preventDefault();
<statements>
}
<form>.addEventListener('submit', <handler>);
| Part | What it does |
|---|---|
<form> | The <form> element. |
'submit' | The event a form fires when it's sent, by a click on its submit button or by Enter in one of its fields. |
event | The event object the browser passes to your function. |
event.preventDefault() | Cancels what the browser would do next: here, sending the form and loading a new page. |
<statements> | Your own handling of the form's data. |
MDN's Event: preventDefault() method page describes it as telling the browser not to take the event's default action.
The Example
The example page has a sign-up form with a name field and a Sign up button. When the form is sent, the script stops the page from reloading and thanks the person by name below the form.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign up</title>
</head>
<body>
<form id="signup">
<label for="name">Name</label>
<input id="name" name="name">
<button type="submit">Sign up</button>
</form>
<p id="message"></p>
<script>
function signUp(event) {
event.preventDefault();
const name = document.getElementById('name').value;
document.getElementById('message').textContent = 'Thanks, ' + name + '! You are signed up.';
}
document.getElementById('signup').addEventListener('submit', signUp);
</script>
</body>
</html>
The listener is on the form, so it runs however the form is sent: by a click on Sign up, or by Enter in the name field.
Step 2: Fill In the Form and Send It
Double-click index.html to open it in your browser. Type Ana into the name field and click Sign up:

The page didn't reload: the name is still in the field, and the address still ends in index.html. The screenshots come from Chromium 159, the open-source browser that Chrome is built on.
What Happens Without preventDefault
Delete the line event.preventDefault();, save the file, reload the page, and send the form again with Ana:

The script still ran and wrote its message, but the browser then sent the form the usual way: it loaded the page again, with the form's data added to the address, index.html?name=Ana. The new page starts empty, so the message and the name are gone.
What Happened
- The
submitevent fired when the button was clicked, and ransignUp. event.preventDefault()cancelled the browser's own sending, so the page wasn't replaced.- The rest of
signUpran on the same page, and its message stayed on screen.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing event.preventDefault without brackets | Nothing is cancelled: the page reloaded with ?name=Ana in the address | Call it: event.preventDefault() |
Listening for click on the button instead of submit on the form | Cancelling the click also cancels the browser's own checks: with a required field left empty, the handler still ran | Listen for submit on the form. It fires only when every field is valid |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The page flashes and your message disappears | The form was sent and the page reloaded | Call event.preventDefault() in the submit handler |
The address gains ?name=... after sending | The browser sent the form the default way | Same fix: event.preventDefault() |
| Your handler never runs | It's attached to the wrong element or event, or the script runs before the form exists | Attach it to the form, with 'submit', from a script after the form |
FAQ
Does pressing Enter run the handler too?
Yes. With the cursor in the name field, pressing Enter sent the form, and the page showed Thanks, Ben! You are signed up. for the name Ben, without reloading.
Where did ?name=Ana come from?
A form without a method sends its data in the address, as name=value pairs after a ?. The field's name attribute, name="name", gave the pair its name.
Can I still check the fields before sending?
Yes. Attributes such as required are checked first: the browser fires submit only when every field is valid.
Conclusion
To handle a form with JavaScript, listen for submit on the form and call event.preventDefault() before anything else. Without it, the browser sends the form and loads a new page, and whatever your script did is lost.
Additional Resources
- Event: preventDefault() method, on MDN.
- HTMLFormElement: submit event, on MDN.
- Sending form data, on MDN.
