In a signup form, pressing Enter in the email field submits the form, and clicking a "Show password" <button> inside it reloads the page. Explain both behaviours and how you would control them.
answer
- button defaults to type submit
- the reload is really a navigation
- Enter activates the default button
- first submit button in tree order wins
- never swallow Enter globally
basics
~20 sBoth are default form behaviour. A <button> with no type attribute defaults to type="submit", so the show-password button submits and navigates. Enter in a text field triggers implicit submission, which activates the form's first submit button. Fix the button with type="button"; keep Enter working.
solid answer
~50 sNeither is a bug in the browser. `<button>` defaults to `type="submit"`, so any button you drop inside a form submits it unless you write `type="button"` — the page "reload" is the resulting navigation to the form's action. Enter in a text field is **implicit submission**: the browser activates the form's default button, the first submit button in tree order, and if the form has no submit button at all it still submits when there is exactly one text-like field. The fixes are asymmetric. The stray button is simply mislabelled markup: give it `type="button"`. Implicit submission, by contrast, is behaviour users depend on — keyboard and screen-reader users expect Enter to submit — so never swallow it with a global keydown handler. If Enter is reaching the wrong action, reorder the submit buttons so the safe one comes first in the DOM, or move the destructive one into its own form.
go deeper
Remember that <button> means submit unless you write type="button", and that pressing Enter in a text field submits the form on purpose.
Explain implicit submission precisely: the default button is the first submit button in tree order, and a form with no submit button still submits when it has exactly one text-like field.
Diagnose the vanished-input report as an unintended navigation, fix it in the markup rather than with key interception, and protect Enter because keyboard and screen-reader users depend on it.
Make it a review rule that every button inside a form declares its type and that destructive actions are unreachable by default submission — through DOM ordering or a separate form — so the pattern cannot regress.
## Why the button submits The `type` attribute of `<button>` defaults to `submit`. That is deliberate — the element exists primarily to submit forms — but it means every unrelated button placed inside a form is a submit button until you say otherwise: ```html <form action="/signup" method="post"> <input id="pw" name="password" type="password"> <button>Show password</button> <!-- submits! --> <button type="button">Show password</button> <!-- correct --> <button>Create account</button> </form> ``` What looks like a page reload is a real navigation: the form posts to its action, the server responds, and the page you get back happens to look similar. The tell is that anything typed is gone and the network panel shows a document request. The same trap applies to `<input type="submit">` versus `<input type="button">`, and to any "button" built from an anchor or a div — those come with their own, worse problems around focusability and keyboard activation, which is exactly why `<button type="button">` is the right tool. ## Why Enter submits **Implicit submission** is the rule that pressing Enter in a text field submits the form without touching a button. The browser looks for the form's **default button** — the first submit button in tree order — and activates it, exactly as if it had been clicked, which means its `name`/`value` enters the payload and any `formaction` on it applies. If the form has no submit button at all, implicit submission still happens when the form has exactly one field of the kinds that block implicit submission (text-like inputs such as `text`, `search`, `email`, `url`, `tel`, `password`, `number` and the date/time family). That is the rule behind the classic "my one-input search box submits on Enter even though there is no button", and behind its mirror image: add a second text field and Enter stops working, because now nothing tells the browser what the default action is. A `<textarea>` is different — Enter inserts a newline there and does not submit, which is what users expect from a multi-line control. ## Fixing each one correctly **The stray button** is a markup mistake with a markup fix: `type="button"`. Do not fix it with `event.preventDefault()` in a click handler, and do not fix it by removing the button from the form. `preventDefault` in a click handler does suppress the submission, but it leaves the element lying about what it is, so the next person who adds a keyboard shortcut or a second handler reintroduces the bug. Write the type. **Implicit submission** must not be "fixed" by intercepting Enter globally: ```js // Do not do this. form.addEventListener("keydown", (e) => { if (e.key === "Enter") e.preventDefault(); }); ``` That breaks a behaviour keyboard and assistive-technology users rely on, and it is one of the more common accessibility regressions in hand-rolled forms. If Enter is doing the *wrong* thing, the problem is which button is default, not that Enter works: - Order the submit buttons so the safe or primary action is **first in the DOM**, and let the styling layer place it visually wherever the design wants. - Move a destructive action such as Delete into its own form, or give it its own endpoint via `formaction`, so implicit submission cannot reach it. - If a form genuinely must not submit on Enter — a filter panel that applies live, say — the honest answer is usually that it should not be a `<form>` with a submit button in the first place. **If the form should have no submit path at all**, still ask whether users can complete it by keyboard. A form that can only be completed by clicking a specific element is a form some people cannot complete. ## Diagnosing it in the wild The symptom "the page reloads and my input is lost" almost always means an unintended submission. Check three things in order: the `type` of every button inside the form, whether a click handler is running alongside a native submission (two code paths, one user action), and which submit button is first in tree order. Adding a temporary `submit` listener that logs `event.submitter` tells you immediately which element the browser thinks triggered it.
- A form has a single text input and no submit button, and it still submits when the user presses Enter. Is that a bug?No, it is implicit submission working as specified. With no submit button, the browser still submits when the form has exactly one field that blocks implicit submission — a text-like input. Add a second such field and Enter stops submitting, which is why forms behave inconsistently as they grow. If you want a dependable Enter, give the form a real submit button.
- Why is preventing the Enter key with a keydown handler the wrong way to stop unwanted submissions?Because it removes a behaviour keyboard and screen-reader users depend on: Enter in a text field is how they submit without hunting for a button. It also masks the real problem, which is usually that the wrong element is the default submit button. Fix the markup instead — set `type="button"` on non-submitting buttons and order submit buttons so the safe one is first.
- How do you tell an accidental submission apart from a genuine page refresh while debugging?An accidental submission is a navigation to the form's action, so the network panel shows a document request with the form payload attached and every typed value is gone afterwards. Attaching a temporary `submit` listener that logs `event.submitter` names the exact element the browser treated as the trigger, which usually turns out to be an untyped `<button>`.
- Where should the primary submit button sit when the design puts it on the right of a secondary one?Put the primary action first in the DOM so implicit submission activates it, and let the styling layer reverse the visual order. DOM order drives the default button and also the tab order, so it should reflect intent; visual placement is a presentation choice that can follow the design independently.
saying these in an interview costs you the question
- Thinks a bare <button> in a form does nothing by default
- Blocks the Enter key globally to stop submissions
- Calls the resulting navigation a browser refresh bug
- Assumes Enter activates the visually primary button
- Uses preventDefault in a click handler instead of type="button"