Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to HTML Notes
Topic #717

button

The <button> element creates a clickable control that performs an action — and inside a form, its type attribute quietly determines whether clicking it submits the form or not.

Syntax

<button type="submit">Submit</button>
<button type="button">Do Something Else</button>
<button type="reset">Clear Form</button>

Basic Example

<form action="/enroll" method="post">
    ...fields...
    <button type="submit">Enroll Now</button>
</form>

Browser output: a styled, clickable button; clicking it (with type="submit") collects the form's data and submits it.

How It Works

TypeBehavior
submitSubmits the parent form — this is the default if type is omitted entirely
buttonDoes nothing on its own — used purely to trigger custom JavaScript behavior
resetClears every field in the form back to its default values

Important Attributes

AttributePurpose
typeControls submit/button/reset behavior — see table above
disabledMakes the button unclickable and visually greyed out

Real-World Example

<button type="button" onclick="togglePasswordVisibility()">Show Password</button>

A genuinely non-submitting button — type="button" is required here, because without it, this button would default to submit and unexpectedly send the form the moment it's clicked.

The Critical Default-Type Gotcha

Inside a <form>, any <button> with no explicit type defaults to submit — a very common source of bugs when a developer adds a button purely for a JavaScript action (like "Show Password" or "Add Another Field") and it unexpectedly submits the whole form instead.

Common Mistakes

  • Forgetting type="button" on a JavaScript-only button inside a form, causing an accidental, unwanted form submission on click
  • Using <a href="#"> styled to look like a button for an action, instead of a real <button> — see the a vs button comparison in Links

Best Practices

  • Always write type explicitly on every button inside a form — never rely on the implicit default
  • Use <button> (not a styled link or div) for any control that performs an action rather than navigating

Accessibility Considerations

A real <button> is automatically keyboard-focusable and activatable with both Enter and Space — a genuine, built-in benefit lost the moment it's replaced with a styled non-button element.

SEO Considerations

No direct ranking effect.

Browser & Modern HTML Notes

<button> (unlike the older <input type="submit">) can contain rich content — an icon plus text, for example — making it the generally preferred modern choice for form buttons.

Mini Practice

  1. Build a form with a real type="submit" button and a separate type="button" for a non-submitting JavaScript action.
  2. Explain, in your own words, what happens if type is left off a button inside a form.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →