Button and Input Types

Use the most specific native HTML control for the job so the browser provides appropriate semantics, validation, keyboard behavior, and device integration. Prefer native `<button>` and `<input>` types over generic elements with custom behavior when they express the intended interaction.

A `<button>` represents an interactive control and should normally be used for actions such as submitting a form, resetting it, or running JavaScript. Its `type` attribute is important: `submit` submits the associated form, `reset` resets form controls, and `button` has no built-in form action. Inside a form, explicitly setting `type="button"` prevents an ordinary action button from accidentally submitting the form.

`<input>` supports several semantic types, including `text`, `email`, `password`, `number`, `date`, `time`, `checkbox`, `radio`, `file`, `search`, `tel`, `url`, `range`, `color`, and `hidden`. The type affects the control's semantics, available UI, keyboard or virtual-keyboard behavior, and built-in constraint validation. For example, `type="email"` enables email-oriented validation, while `type="date"` communicates that the value is a date and can provide a date picker in supporting browsers.

Use the narrowest appropriate input type rather than treating every value as plain text. For example, use `email` for an email address, `url` for a URL, `tel` for telephone input, `search` for a search field, and `number` when the value is genuinely numeric. A telephone number is not the same kind of data as a mathematical number, so `tel` is generally more appropriate for phone numbers.

Native controls also participate in accessibility and browser behavior without requiring you to recreate those features. Pair form controls with `<label>` elements, use `name` when a control needs to contribute a form value, and use attributes such as `required`, `min`, `max`, `step`, `minlength`, `maxlength`, and `pattern` where their constraints match the data. Client-side validation improves the interface but does not replace server-side validation for submitted data.

Key points

Interview questions

What are the main types of HTML `<button>`?

The common button types are `submit`, `reset`, and `button`. `submit` submits its associated form, while `reset` restores form controls to their initial values. `button` has no default form action and is commonly used for JavaScript-driven interactions. In forms, explicitly choosing the intended type avoids accidental submissions.

Why should you use `type="email"` instead of `type="text"` for an email field?

`type="email"` communicates that the value represents an email address. Browsers can apply email-specific constraint validation and may provide an input experience suited to email entry on some devices. It also gives assistive technology and other user agents more semantic information. It does not make server-side validation unnecessary.

Which input type would you choose for a phone number, and why?

I would normally use `type="tel"` for a phone number. A phone number is generally an identifier or contact value rather than a quantity on which arithmetic is performed, so `number` does not model it well. `tel` can also encourage an appropriate telephone-oriented virtual keyboard on supporting devices. Additional formatting and validation requirements should be handled according to the application's needs.

How do native input types improve frontend development?

Native types provide semantics that browsers and assistive technologies can understand without custom implementation. Depending on the type, the browser can supply constraint validation, specialized controls, or an appropriate virtual keyboard. This reduces custom JavaScript and accessibility work. Developers still need to label controls correctly and validate untrusted data on the server.

Why is `type="button"` important for buttons inside forms?

A button associated with a form can have submission behavior when its type is not explicitly chosen. If a control is intended only to open a dialog, toggle UI, or trigger client-side behavior, accidental submission is a correctness bug. Declaring `type="button"` makes that intent explicit and avoids relying on implicit behavior. This is especially useful in reusable components that may later be placed inside forms.