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".
_10<form>_10 <input name="email" type="email" />_10 <!-- Does not submit the form -->_10 <button type="button" id="check">Check availability</button>_10 <!-- Submits the form -->_10 <button type="submit">Sign up</button>_10</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.
_10function Button({ type = "button", ...props }: React.ComponentProps<"button">) {_10 return <button type={type} {...props} />;_10}
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.
_10const form = document.querySelector("#signup");_10_10form.addEventListener("submit", async (event) => {_10 event.preventDefault();_10 const data = new FormData(form);_10 await fetch("/api/signup", { method: "POST", body: data });_10});
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:
_14function SignupForm() {_14 function handleSubmit(event: React.FormEvent<HTMLFormElement>) {_14 event.preventDefault();_14 const data = new FormData(event.currentTarget);_14 // send data_14 }_14_14 return (_14 <form onSubmit={handleSubmit}>_14 <input name="email" type="email" required />_14 <button type="submit">Sign up</button>_14 </form>_14 );_14}
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
preventDefaultin a button's click handler and forgetting that Enter in a text field still submits the form. - Returning
falsefrom anaddEventListenercallback. That does nothing; only inlineonsubmitattributes treat afalsereturn value as a cancel. - Throwing an error inside the submit handler before
preventDefaultruns. The handler stops and the browser carries on with the submission, so the error flashes in the console and disappears on reload. CallpreventDefaultfirst. - 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
#HTMLquick question
Was this resource helpful?