html submit onclick - <button> vs. <input type=“button” />. Which to use?
Just as a side note,
<button> will implicitly submit, which can cause problems if you want to use a button in a form without it submitting. Thus, another reason to use
<input type="button"> (or
Edit - more details
Without a type,
button implicitly receives type of
submit. It does not matter how many submit buttons or inputs there are in the form, any one of them which is explicitly or implicitly typed as submit, when clicked, will submit the form.
There are 3 supported types for a button
submit || "submits the form when clicked (default)" reset || "resets the fields in the form when clicked" button || "clickable, but without any event handler until one is assigned"
When looking at most sites (including SO), most of them use:
<input type="button" />
- What are the main differences between the two, if any?
- Are there valid reasons to use one instead of the other?
- Are there valid reasons to use combine them?
- Does using
<button>come with compatibility issues, seeing it is not very widely used?
<button> element you can put content, like text or images.
<button type="button">Click Me!</button>
This is the difference between this element and buttons created with the
Use button from input element if you want to create button in a form. And use button tag if you want to create button for an action.
Quoting the Forms Page in the HTML manual:
Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content.
As far as CSS styling is concerned the
<button type="submit" class="Btn">Example</button> is better as it gives you the ability to use CSS
:after pseudo classes which can help.
Due to the
<input type="button"> visually rendering different to an
<span> when styled with classes in certain situations I avoid them.
It's very worth noting the current top answer was written in 2009. IE6 isn't a concern now days so
<button type="submit">Wins</button> styling consistency in my eyes comes out on top.
- by default behaves like if it had a "type="submit" attribute
- can be used without a form as well as in forms.
- text or html content allowed
- css pseudo elements allowed (like :before)
- tag name is usually unique to a single form
- type should be set to 'submit' to behave as a submitting element
- can only be used in forms.
- only text content allowed
- no css pseudo elements
- same tag name as most of the forms elements (inputs)
in modern browsers, both elements are easily styleable with css but in most cases,
button element is preferred as you can style more with inner html and pseudo elements
I just want to add something to the rest of the answers here. Input elements are considered empty or void elements (other empty elements are area , base , br , col , hr , img , input , link , meta , and param. You can also check here), meaning they cannot have any content. In addition to not having any content, empty elements cannot have any pseudo-elements like ::after and ::before, which I consider a major drawback.