Skip to main content

How to Stop a Button from Submitting a Form in HTML

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Every <button> inside a <form> sends the form when it's clicked, unless you say otherwise. Add type="button" and it becomes a plain button that leaves the form alone, ready for a script to give it a job.

Syntax​

Syntax
<button type="button"><text></button>
PartWhat it does
type="button"Makes it a plain button: clicking it doesn't send the form. A script can still listen for its clicks.
<text>The text on the button.

Without type, a button in a form is a submit button: Chromium reported submit as the type of a button that has no type attribute. MDN's button page lists the three types, submit, reset and button, and confirms that submit is the default.

The Example​

The example form has a text box and two buttons: Check spelling, with type="button", and Send, which sends the form. You click Check spelling and the text stays. Then you take the type away and click it again, to see what changes.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding a label Name, a text box with the name name, a Check spelling button of type button and a Send button of type submit.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Button types</title>
</head>
<body>
<form>
<label for="name">Name</label>
<input id="name" name="name">
<button type="button">Check spelling</button>
<button type="submit">Send</button>
</form>
</body>
</html>

Step 2: Type a Name and Click Check spelling​

Double-click index.html to open it in your browser. Type Ana and click Check spelling:

The page in Chromium after clicking Check spelling: the label Name, the text box still showing Ana, and the Check spelling and Send buttons.

Nothing happens, and that's the point: the text box still says Ana, and the address still ends with index.html. In a real page, a script would check the spelling here.

Step 3: Take the type Away and Try Again​

In index.html, change <button type="button">Check spelling</button> to <button>Check spelling</button> and save the file. Reload the page, type Ana again and click Check spelling:

The page in Chromium after clicking the button without a type: the text box is empty again, next to the Check spelling and Send buttons.

This time the button sent the form: the page loaded again with an empty text box, and the address ended with ?name=Ana. The screenshots come from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • type="button" made Check spelling a plain button, so clicking it left the form alone.
  • Without type, it was a submit button, the default for a button in a form. Chromium reported the type of both buttons as submit, so either one sent the form.
  • Sending the form loaded the page again, which is why the text box was empty.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out type on a button that shouldn't send the formClicking it sends the form and loads the page again, so what people typed is goneAdd type="button"
Writing type="button" on the button that should send the formClicking it sends nothingUse type="submit" for the send button
Using type="reset" for a Clear button that people may click by accidentA test showed it puts every field back to its starting value without askingLeave it out, or make sure people can't mistake it for Send

Troubleshooting​

SymptomLikely causeFix
The page reloads when a button is clickedThe button has no type, so it's a submit buttonAdd type="button"
The form isn't sentThe send button has type="button"Change it to type="submit"
?name=... appears in the address after a clickA button without a type sent the formAdd type="button" to that button

FAQ​

Can a script still react to a type="button" button?​

Yes. type="button" only stops it from sending the form: a click listener added to the button runs as usual.

What does type="reset" do?​

It puts every field in the form back to its starting value. In a test, a text box that started with Ana was changed to Ben, and clicking a type="reset" button brought back Ana, without sending the form.

Does a button outside a form need a type?​

A button that isn't in a form has no form to send: in a test, clicking one without a type left the page and the text box just as they were. Adding type="button" anyway is a good habit, in case the button ends up inside a form later.

Conclusion​

A button in a form sends the form unless it has type="button". Give every button its type, submit for the one that sends and button for the rest, and no click sends the form by surprise.

Additional Resources​