The HTML button type decides whether a button runs an interface action, submits its associated form, or resets form controls. If a Preview button unexpectedly sends a request or a Cancel button triggers validation, inspect its rendered type and form association first. An ordinary button inside a form can act as a submit button when its type is missing.
Before launching a student project, portfolio editor, or public signup page, test secondary actions as carefully as the main submit action. This guide builds a small local preview form and checks that only the intended action reaches its submit handler. It does not send data to a server.
Choose the HTML button type by behavior
Use an explicit type for each ordinary form button:
type="button"runs no native submit or reset action. Use it for Preview, Add another field, or a custom Cancel handler.type="submit"requests submission of the associated form. Use it for Save, Search, or Send when that is the intended action.type="reset"restores form controls to their default values. Use it only when that behavior is deliberate and clearly labeled.
The MDN button reference documents these behaviors. Do not choose a type from the button's color, position, or label. A button labeled “Cancel” still needs the correct behavior, and type="button" alone does not implement cancellation.
A destination such as “Back to projects” usually belongs in an anchor with an href. Use the website CTA checklist for the broader choice between navigation links and commands.
Find why a button submits a form unexpectedly
Reproduce the failure with both an empty required field and valid input. A missing type can be hidden by native validation: an unintended submission attempt might show an error instead of making a request.
Inspect the actual DOM in DevTools, select the failing button, and run:
({
declaredType: $0.getAttribute("type"),
effectiveType: $0.type,
formId: $0.form?.id,
formAction: $0.form?.action,
overrideAction: $0.getAttribute("formaction"),
})
This snippet assumes $0 is the selected HTMLButtonElement. The declared attribute and effective browser property may differ. Check the form owner even when the button appears outside the form: a form attribute can associate it with another element by ID.
Fix the source component rather than editing DevTools. If you use a reusable React button, verify that it forwards type to the native element and allows submit buttons to opt in. Inspect the final markup after rendering; a correct prop at the call site is insufficient if the component drops or overwrites it.
Do not use stopPropagation() as a substitute for choosing the type. Stopping event propagation and cancelling a default action are different operations, as the DOM event documentation explains. Also inspect click handlers for explicit requestSubmit(), submit(), or API calls. Correcting the native type cannot remove a submission your own JavaScript starts.
Build a preview action that preserves the form
Serve this HTML as /button-demo.html in a local test environment. It contains a required field, a secondary preview command, and one submit button. The preview reports the current title without clearing it or entering the submit handler.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Project title preview demo</title>
</head>
<body>
<main>
<h1>Review a project title</h1>
<form id="project-form">
<label for="project-title">Project title</label>
<input id="project-title" name="title" required>
<button id="preview-title" type="button">Preview title</button>
<button id="save-title" type="submit" name="intent" value="save">
Save project
</button>
</form>
<p id="preview-result" role="status"></p>
<p id="submit-result" role="status" data-count="0"></p>
</main>
<script>
const form = document.querySelector("#project-form");
const title = document.querySelector("#project-title");
const preview = document.querySelector("#preview-result");
const result = document.querySelector("#submit-result");
document.querySelector("#preview-title").addEventListener("click", () => {
preview.textContent = title.value.trim()
? `Preview: ${title.value.trim()}`
: "Enter a title to preview.";
});
form.addEventListener("submit", (event) => {
event.preventDefault(); // Local demo: never send a request.
const data = new FormData(form, event.submitter);
result.dataset.count = String(Number(result.dataset.count) + 1);
result.textContent =
`Handled ${data.get("intent")}: ${data.get("title")}`;
});
</script>
</body>
</html>
Use textContent to display this user-entered title as text. The submit listener owns the demo's submission handling, so clicking the submit button and pressing Enter can reach the same handler. Production code must validate and persist data on the server and show success only after the intended operation succeeds. The website form testing checklist covers those request, failure, and recovery checks.
The submitter identifies which submit control initiated the operation. Passing it to the FormData constructor includes its name/value pair when applicable. This matters when a real form has separate “Save draft” and “Publish” actions. Do not assume every submission has a submitter, or infer a destructive action when the value is absent.
Test Enter-key submission and native validation
Clicking Save is only one path. The HTML Standard's implicit submission rules describe how supporting browsers activate a form's default submit button when the user submits implicitly, such as by pressing Enter in a text field. The default is the first associated submit button in tree order, not necessarily the most prominent button on screen.
For the demo, Enter in the title field should choose Save project. Preview must remain a non-submit action even though it appears first. For forms with several submit buttons, define the expected default action and test it explicitly. Avoid a default destructive action.
Do not add a blanket Enter-key cancellation handler to hide the bug. It can prevent expected keyboard submission. Keep multiline text entry working in textareas and test any custom combobox key handling separately. Use the keyboard accessibility testing guide for focus order and widget-specific behavior.
Native required-field validation runs before this demo's submit event. Confirm that an empty Save attempt leaves the handler count unchanged, while valid input reaches it once. Review the browser's feedback manually and use the accessible form errors guide if you replace native validation with custom messages.
When JavaScript needs to initiate a normal submission, requestSubmit() participates in validation and submit-event handling. Calling form.submit() directly skips those steps. Do not use that shortcut to make a failing browser test pass.
Add an HTML button type regression test
Save this as a Playwright test in an existing Playwright project. Set BUTTON_DEMO_URL to the local page's full URL before running it. The test checks an observable side effect, the demo handler count, instead of merely asserting that the attribute exists.
import { expect, test } from "@playwright/test";
for (const width of [375, 1280]) {
test(`button actions at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 900 });
await page.goto(process.env.BUTTON_DEMO_URL!);
const title = page.getByLabel("Project title", { exact: true });
const preview = page.getByRole("button", { name: "Preview title" });
const save = page.getByRole("button", { name: "Save project" });
const handled = page.locator("#submit-result");
await title.fill("Launch checklist");
await preview.click();
await expect(page.locator("#preview-result"))
.toHaveText("Preview: Launch checklist");
await expect(title).toHaveValue("Launch checklist");
await expect(handled).toHaveAttribute("data-count", "0");
await title.fill("Keyboard preview");
await preview.focus();
await page.keyboard.press("Space");
await expect(page.locator("#preview-result"))
.toHaveText("Preview: Keyboard preview");
await expect(handled).toHaveAttribute("data-count", "0");
await title.fill("");
await save.click();
await expect(title).toBeFocused();
await expect(handled).toHaveAttribute("data-count", "0");
await title.fill("Launch checklist");
await title.press("Enter");
await expect(handled).toHaveAttribute("data-count", "1");
await expect(handled).toHaveText("Handled save: Launch checklist");
await save.click();
await expect(handled).toHaveAttribute("data-count", "2");
});
}
Playwright supports keyboard input through locators and the page keyboard. Run the test in the browsers you support. These widths exercise the same action contract at two layouts; they do not prove touch usability, visible focus quality, or screen-reader announcements.
For an application with a backend, extend the test with a controlled endpoint or intercepted request: Preview should send no save request, and Save should send the expected payload once. Test server validation, pending-state duplicate prevention, and retry behavior separately. The demo intentionally allows two completed submissions so the test can distinguish Enter activation from a later click.
Check the deployed public page with Slop Audit
After fixing the button behavior, run Slop Audit on the public form page. Review the available form-label findings, axe accessibility evidence, sampled Tab focus, narrow-layout clipping, and screenshots. Fix observed barriers and rescan the same route after deployment.
Slop Audit's bounded first-task evaluator excludes form-associated buttons from activation. It does not submit this form, verify the selected submitter, test implicit Enter submission, or confirm a saved backend record. Keep the regression test above alongside the public-page audit so your launch evidence covers both the form's behavior and its surrounding interface.