A user opens an edit form and types a new name. Then they change their mind and click Cancel. The page reloads, the field is empty, and the address bar now ends in ?name=.... Cancel did not cancel. It sent the form.
Nothing is wrong with the click handler. The button itself has a default job, and that job is "submit."
The markup
Here is a small edit form. Cancel is meant to close it. Save is meant to send it.
<form id="edit">
<label>Name <input id="name" name="name"></label>
<button id="cancel">Cancel</button>
<button id="save" type="submit">Save</button>
</form>
document.getElementById("cancel").addEventListener("click", () => {
closeEditor();
});
Save says type="submit". Cancel says nothing at all. That missing attribute is the whole bug.
What we saw in Chrome
We ran this in headless Chrome 156 on November 16, 2026. We typed "Ana Lima" into the field and clicked Cancel.
- The click handler ran, and then the form was submitted.
- The page navigated to
reload.html?name=Ana+Lima. - After the reload, the field was empty and the page was back to its starting state.
Then we added type="button" to Cancel and ran it again. The click handler ran. There was no navigation, and "Ana Lima" stayed in the field.
We also logged event.submitter in a submit listener. That property names the button that started the submit. For the first form, it named Cancel.
The Enter key can press Cancel too
This part surprises people more. We put the cursor in the text field and pressed Enter. Chrome fired a click on Cancel, not on Save. The form was then submitted with Cancel as the submitter.
The reason is the "default button." The HTML standard says it is the first submit button in tree order, which means source order. When a user submits a form with Enter, the browser fires a click on that default button. MDN's page on the submit event says the same.
Cancel had no type, so it counted as a submit button. It came before Save in the markup, so it became the default button. Any code in its click handler ran on Enter.
With type="button" on Cancel, Enter clicked Save instead. That is what most people expect.
Why a plain <button> submits
MDN's <button> page lists three values for type:
-
submitsends the form. It is the default when the attribute is missing on a button tied to a form. It is also the default for an empty or invalid value. -
resetputs every field back to its starting value. -
buttonhas no built-in action. Only your own scripts do something.
The same page has a plain warning. If a button is not for sending form data, set its type to button.
Our Chrome test matched each of these points:
-
type=""submitted the form. -
type="cancel", which is not a real value, submitted the form. -
type="BUTTON"in capitals did not submit. The value is not case-sensitive. -
type="reset"cleared the field back to its starting text. - Reading
.typein JavaScript gave"submit"for a button with no attribute, an empty value, or an invalid value.
Outside a form, and the form attribute
A button with no type that sits outside every form did not submit anything in our test. Its .type still read "submit". There was simply no form to send.
That changes if the button points at a form with the form attribute. We placed a typeless button outside the form and gave it form="fE". Clicking it submitted that form. So "it is not inside the form" is not proof that it is safe.
The fix
Give every button an explicit type.
<form id="edit">
<label>Name <input id="name" name="name"></label>
<button id="cancel" type="button">Cancel</button>
<button id="save" type="submit">Save</button>
</form>
Some teams call event.preventDefault() inside the Cancel click handler instead. That stops the submit, but the button is still a submit button. If Cancel comes first, Enter still runs the Cancel handler. We tested that too. Enter ran the Cancel code, and Save was never clicked. The type fix removes the cause, not just the symptom.
A short checklist
- Inside a form, a
<button>with notypebehaves liketype="submit". - An empty or misspelled
typebehaves the same way. - Enter in a text field clicks the first submit button in the form. Check which button that is.
- A button outside the form can still submit it through the
formattribute. - Use
type="button"for Cancel, Close, Show password, Add row, and every other button that should not send data.
On MDN, the <button> element is marked Baseline widely available from July 2015. SubmitEvent.submitter is marked Baseline widely available from March 2022.
Sources
- MDN, the button element: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button
- MDN, HTMLButtonElement type property: https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement/type
- MDN, HTMLFormElement submit event: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event
- MDN, SubmitEvent submitter property: https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent/submitter
- HTML Living Standard, implicit submission: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission
PIXEL RECRUITING LLC introduces frontend and UI engineers to product teams that care about details like this one. We mean a Cancel button that really cancels, even when someone presses Enter. We are a direct staffing partner for Mastercard in O'Fallon, MO. Learn more at pixelrecruiting.com.
Top comments (0)