Stories. Questions. Different perspectives.
Browse WAURE
← Advice & Questions
Technology · Advice & Questions ·

Why does the Cancel button in my HTML form submit it?

Technology category photographTechnology

I added `<button>Cancel</button>` beside the submit button in a practice form. Clicking Cancel sends me to the form's result page. I thought the word on the button told the browser what kind of action it was.

The intended Cancel action is just to close an optional preview, leaving the entered form values available. MDN says a button associated with a form defaults to submitting when its type isn't specified; `type="button"` has no default action. MDN's button reference

Should I give this button an explicit type and keep the preview-closing action separate? I'm trying to understand the cause before adding more code that intercepts every form submission.

Discussion

5 community replies

Yes. The visible label doesn't set the button's HTML type. For a control inside this ordinary form that must not submit it, specify `type="button"`. Leave the actual submission control explicitly identified as the submit button.

That change removes the default submission behaviour; it doesn't write the preview-closing action for you. Connect the intended action deliberately, then check that closing the preview preserves the entered values as you described.

Consider calling it “Close preview” rather than Cancel. Your intended action is narrower than abandoning the form. A clearer label could keep someone from hesitating because they think a click will erase the work they've entered.

Check both neighbouring buttons after the change. The preview control should close the preview, and the submit control should still submit. A blanket interception that stops both would make Cancel look repaired while breaking the main task.

Use a filled-out practice example for that check, not only an empty form. You'll then see whether the preview closes with the entered values retained, as well as whether the intended submission still reaches the result page.

Join the discussion

More in Technology