---
title: "Why does my form reload the page when I click a button?"
description: "Clicking a button inside a form reloads the page because buttons default to type submit. Learn how to stop it with type button or preventDefault."
url: https://www.danny.engineering/answer/html/why-does-my-form-reload-the-page-when-i-click-a-button
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["HTML"]
---

# Why does my form reload the page when I click a button?

## The short answer


A `<button>` inside a `<form>` is a submit button unless you say otherwise. Clicking it submits the form, and with no JavaScript in the way the browser sends a request to the form's `action` (or the current URL) and loads the response, which looks like a page reload. Give buttons that are not meant to submit `type="button"`.


```html
<form>
  <input name="email" type="email" />
  <!-- Does not submit the form -->
  <button type="button" id="check">Check availability</button>
  <!-- Submits the form -->
  <button type="submit">Sign up</button>
</form>
```


## Why it happens


The HTML specification gives the `type` attribute of a button three values: `submit`, `reset` and `button`. If the attribute is missing, or has a value the browser does not recognise, the button behaves as `submit`. That default made sense when forms were the main way to send data, but it surprises people who add a button to a form to open a menu, toggle a password field or run some JavaScript.


When a form is submitted the browser fires a `submit` event on the form. If nothing cancels that event, the browser performs a navigation. For a form with no `action` attribute, that navigation goes to the current page, often with the field values added to the URL as a query string. Your JavaScript state is thrown away and the page starts again.


There is a second route to the same result: pressing Enter in a text field. This is called implicit submission. If the form has a submit button, or has only one text field, Enter submits it, even if you never click anything.


## How to fix it


### Set the right type on every button


The simplest and most reliable fix is to be explicit. Use `type="button"` for buttons that only run JavaScript, and `type="submit"` for the one that sends the form. Writing the type on every button, even submit buttons, makes the intent clear to the next person reading the code.


This matters in component libraries too. A shared `Button` component that renders a plain `<button>` without a type will submit any form it is placed in.


```typescript
function Button({ type = "button", ...props }: React.ComponentProps<"button">) {
  return <button type={type} {...props} />;
}
```


### Handle the submit event and call preventDefault


When you do want the form to submit, but you want to handle the data in JavaScript (for example, sending it with `fetch`), listen for the form's `submit` event and cancel the default navigation.


```javascript
const form = document.querySelector("#signup");

form.addEventListener("submit", async (event) => {
  event.preventDefault();
  const data = new FormData(form);
  await fetch("/api/signup", { method: "POST", body: data });
});
```


Listen on the form, not on the button's `click` event. The `submit` event also fires for implicit submission with the Enter key, and it only fires after built-in validation such as `required` and `type="email"` has passed. A click handler on the button misses both.


In React, the same idea looks like this:


```typescript
function SignupForm() {
  function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    // send data
  }

  return (
    <form onSubmit={handleSubmit}>
      <input name="email" type="email" required />
      <button type="submit">Sign up</button>
    </form>
  );
}
```


If you use React 19 form actions (passing a function to the form's `action` prop), React handles the submission for you and you do not need `preventDefault`.


### Move the button outside the form


If a button has nothing to do with the form, it may not belong inside it. A button outside any form has nothing to submit, so its default type does no harm. Only do this when it suits the layout and the meaning of the page; changing the type is usually the better fix.


## Common pitfalls

- Calling `preventDefault` in a button's click handler and forgetting that Enter in a text field still submits the form.
- Returning `false` from an `addEventListener` callback. That does nothing; only inline `onsubmit` attributes treat a `false` return value as a cancel.
- Throwing an error inside the submit handler before `preventDefault` runs. The handler stops and the browser carries on with the submission, so the error flashes in the console and disappears on reload. Call `preventDefault` first.
- Removing the form element to stop reloads. You lose Enter to submit, built-in validation and the accessible grouping of fields. Keep the form and handle `submit`.
- Forgetting that `type="reset"` buttons clear every field. They are rarely wanted.

## Related reading

- [MDN: The button element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)
- [MDN: HTMLFormElement submit event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event)
