Skip to main content

How to Stop a Form from Reloading the Page with preventDefault in JavaScript

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
function <handler>(event) {
event.preventDefault();
<statements>
}

<form>.addEventListener('submit', <handler>);
PartWhat 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.
eventThe 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:

index.html, a page with a form with the id signup holding a Name label, a text box named name and a Sign up submit button, an empty paragraph with the id message, and a script with a signUp function that calls event.preventDefault, reads the name and writes a thank-you message, attached to the form's submit event.

index.html
<!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 in Chromium: the Name field still holding Ana, the Sign up button, and below them Thanks, Ana! You are signed 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 page in Chromium after sending the form without preventDefault: an empty Name field and the Sign up button, with no message below.

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 submit event fired when the button was clicked, and ran signUp.
  • event.preventDefault() cancelled the browser's own sending, so the page wasn't replaced.
  • The rest of signUp ran on the same page, and its message stayed on screen.

Common Mistakes​

MistakeWhat happensDo this instead
Writing event.preventDefault without bracketsNothing is cancelled: the page reloaded with ?name=Ana in the addressCall it: event.preventDefault()
Listening for click on the button instead of submit on the formCancelling the click also cancels the browser's own checks: with a required field left empty, the handler still ranListen for submit on the form. It fires only when every field is valid

Troubleshooting​

SymptomLikely causeFix
The page flashes and your message disappearsThe form was sent and the page reloadedCall event.preventDefault() in the submit handler
The address gains ?name=... after sendingThe browser sent the form the default waySame fix: event.preventDefault()
Your handler never runsIt's attached to the wrong element or event, or the script runs before the form existsAttach 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​