64

I started using a diagnostic css stylesheet, e.g. http://snipplr.com/view/6770/css-diagnostics--highlight-deprecated-html-with-css--more/

One of the suggested rules highlights input tags with the type submit, with the recommendation to use <button> as a more semantic solution. What are the advantages or disadvantages of <button> with type submit (such as with browser compatibility) that you have run across?

Just to be clear, I understand the spec of <button>, it has a defined start and end, it can contain various elements, whereas input is a singlet and can't contain stuff. What I want to know essentially is whether it's broken or not. I'd like to know how usable button is at the current time. The first answer below does seem to imply that it is broken for uses except outside of forms, unfortunately.

Edit for 2015

The landscape has changed! I have 6 more years experience of dealing with button now, and browsers have somewhat moved on from IE6 and IE7. So I'll add an answer that details what I found out and what I suggest.

Kzqai
  • 21,270
  • 21
  • 97
  • 131
  • 4
    Have you noticed how one never says "if you want Opera9" or "if you want Firefox 2" or... lol IE6/7 :( – ANeves thinks SE is evil Oct 01 '10 at 10:36
  • See the update to my answer. There is a script that seemingly makes the button tag more usable on IE6/7. – slolife Feb 10 '11 at 08:53
  • 6
    @ANeves That is because IE6 and 7 have significant market share, while Opera9 and Firefox 2 do not. Pretty simple stuff. – meager Apr 20 '11 at 18:02
  • Another quirk I eventually found with the button tag: if you wrap it in links, that works in some browsers and not in other, less capable ones (ie6, ie7, ie8). That's probably not just specific to button, though where else would the technique be as useful? – Kzqai Dec 03 '11 at 14:25
  • Probably just anything that doesn't support html5 links around various eelements like block level elements won't allow links around the button element. – Kzqai Dec 03 '11 at 15:52
  • this question could use an update – Randy L Oct 07 '15 at 19:44
  • 1
    @the0ther updated as best I could. – Kzqai Oct 08 '15 at 00:27

11 Answers11

49

When using <button> always specify the type, since browsers default to different types.

This will work consistently across all browser:

  • <button type="submit">...</button>
  • <button type="button">...</button>

This way you gain all of <button>'s goodness, no downsides.

orip
  • 66,082
  • 20
  • 111
  • 144
  • 1
    +1 - although this is far away from being the answer (since the OP was asking for something else), this is great, great advice. – AeroCross Oct 09 '12 at 19:19
17

Answering from an ASP.NET perspective.

I was excited when I found this question and some code for a ModernButton control, which, in the end, is a <button> control.

So I started adding all sorts of these buttons, decorated with <img /> tags inside of them to make them stand out. And it all worked great... in Firefox, and Chrome.

Then I tried IE6 and got the "a potentially dangerous Request.Form value was detected", because IE6 submits the html inside of the button, which, in my case, has html tags in it. I don't want to disable the validateRequest flag, because I like this added bit of data validation.

So then I wrote some javascript to disable that button before the submit occurred. Worked great in a test page, with one button, but when I tried it out on a real page, that had other <button> tags, it blew up again. Because IE6 submits ALL of the buttons' html. So now I have all sorts of code to disable buttons before submit.

Same problems with IE7. IE8 thankfully has this fixed.

Yikes. I'd recommend not going down this road IF you are using ASP.NET.

Update:

I found a library out there that looks promising to fix this.

If you use the ie8.js script from this library: http://code.google.com/p/ie7-js/

It might work out just fine. The IE8.js brings IE5-7 up to speed with IE8 with the button tag. It makes the submitted value the real value and only one button gets submitted.

Community
  • 1
  • 1
slolife
  • 18,415
  • 19
  • 75
  • 116
  • 3
    This parallels what happened to me. > "Man, using robust buttons to submit forms instead of inputs would be great!" > "Uh oh, the problems that I knew about were only part of a longer list." > "Hmmm, button is effectively useless, more trouble than it's worth, now I've got a few buttons to revert." – Kzqai Feb 08 '10 at 06:28
  • I actually tried that IE7 js library, and was not impressed. Use with caution. – Kzqai Apr 20 '11 at 18:33
  • @Tchalvak, can you expand on what problems/concerns you had wit hthe IE7 js library? – slolife Sep 20 '11 at 18:41
  • Javascript errors in it's targetted ie7 at the time I checked it out. – Kzqai Sep 21 '11 at 13:05
  • 3
    All this Microsoft stuff == Brain damage – Dmitry Minkovsky Jul 06 '13 at 05:01
13

Everything you need to know: W3Schools <button> Tag

The tag is supported in all major browsers.

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.

Community
  • 1
  • 1
David Brown
  • 32,652
  • 11
  • 79
  • 120
  • 6
    wow... doesn't that just make practical use of the button tag impossible? – hitec Dec 14 '09 at 21:18
  • 4
    Is that saying "you can use the button tag, as long as it's isn't in an html form"? Ouch, I guess if I had any use for button -besides- form buttons, that'd be great, but I guess this means that mainly I'll be sticking with the tag. – Kzqai Dec 14 '09 at 21:34
  • 2
    For forms, I would just stick to the `` tag because it seems to be the most consistent of the two. If you absolutely have to use ` – David Brown Dec 14 '09 at 21:44
  • 8
    If you're triggering AJAX, there's no reason you need a form. Button works splendidly in that situation. – Stefan Kendall Dec 14 '09 at 21:52
  • 3
    @stefan, so does any element bound to a click() function :) – Jason Dec 14 '09 at 22:20
  • ``, IE will post `abutton=Submit Me!`, whereas most other browsers will post `abutton=submit`. – Duroth Dec 15 '09 at 08:39
  • 21
    I've edited the answer to remove the implication that the W3Schools page was a W3C page. The two organizations are not associated with each other. The W3C writes the standards. W3Schools writes low quality tutorials and references about them. – Quentin Feb 05 '10 at 07:30
  • 1
    @Stefan — if you are triggering Ajax then you usually do still need a form. See rule two: http://icant.co.uk/articles/pragmatic-progressive-enhancement/#build – Quentin Feb 05 '10 at 07:31
  • @hitec — No ... but another bug in Internet Explorer (where button elements are *always* successful, even if they weren't clicked) means that for anything other than the simple "This form has one submit button", it is. – Quentin Feb 05 '10 at 07:33
  • 3
    I guess the moral here is that we should not wait for ie6 users any more, they're living in a broken facsimile of the web anyway. – Kzqai Sep 07 '11 at 14:52
10

Pros:

  • The display label does not have to be the same as the submitted value. Great for i18n and "Delete this row"
  • You can include markup such as <em> and <img>

Cons:

  • Some versions of MSIE default to type="button" instead of type="submit" so you have to be explicit
  • Some versions of MSIE will treat all <button>s as successful so you can't tell which one was clicked in a multi-submit button form
  • Some versions of MSIE will submit the display text instead of the real value

From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button:

IE7 has a bug where when submitting a form with Click me, the POST data sent will result in myButton=Click me instead of myButton=foo. IE6 has an even worse bug where submitting a form through a button will submit ALL buttons of the form, with the same bug as IE7. This bug has been fixed in IE8.

Kenston Choi
  • 2,604
  • 1
  • 24
  • 35
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 1
    This answer is one of the best summaries of the conversation here, except that it lacks IE version number ranges for the 3 cons. – jpaugh Jan 28 '16 at 17:17
9

An important quirk to be aware of: In a form that contains a <button/> element, IE6 and IE7 will not submit the form when the <button/> element is clicked. Other browsers, on the other hand, will submit the form.

In contrast, no browsers will submit the form when <input type="button"/> or <button type="button"/> elements are clicked. And naturally, all browsers will submit the form when <input type="submit"/> or <button type="submit"/> elements are clicked.

As @orip's answer says, to get consistent submit behavior across browsers, always use <button type="button" /> or <button type="submit" /> inside a <form/> element. Never leave out the type attribute.

Chris Calo
  • 6,567
  • 7
  • 44
  • 57
5

I've had some experience with the quirks of <button> now, 6 years later, so here are my suggestions:

  • If you're still supporting IE6 or IE7, be very careful with button, the behavior is very buggy with those browsers, in some cases submitting the innerHtml instead of value='whatever' and all button values instead of just one and wonky behavior like that. So test thoroughly or avoid for those browser's sake.

  • Otherwise: If you're still supporting IE8, <a href='http://example.com'><button></button></a> doesn't work well, and probably anything else where you nest a button inside a clickable element. So watch out for that.

  • Otherwise: If you're using a <button> mainly as an element to click for your javascript, and it's outside of a form, make it <button type='button'> and you'll probably be just fine!

  • Otherwise: If you're using <button> in a form, be wary that the default type of <button> is actually <button type='submit'> in (most) cases, so be explicit with your type and your value, like: <button type='submit' value='1'>Search</button>.

Note that: Using a button-mimic class, like Bootstrap's .btn allows you to just make things like <div> or <a> or even <button> look exactly the way you want it to, and in the case of <a> have a more useful fallback behavior. Not a bad option.

TLDR; Ok to use if you don't care about ancient browsers, but Bootstrap provides even more robust css visually similar alternatives worth looking into.

Kzqai
  • 21,270
  • 21
  • 97
  • 131
4

Is it broken or not:

As usual, the answer is "it works fine in all major browsers, but has the following quirks in IE." I don't think it will be a problem for you though.

The <button> tag is supported by all the major browsers. The only support problem lies in what Internet Explorer will submit upon pressing a button.

The major browsers will submit the content of the value attribute. Internet exploter will submit the text between the <button> and </button> tags, while also submitting the value of every other one in the form, instead just the one you clicked.

For your purposes, just cleaning up old HTML, this shouldn't be a problem.

Sources:

  1. http://www.peterbe.com/plog/button-tag-in-IE
  2. http://www.w3schools.com/tags/default.asp
Kzqai
  • 21,270
  • 21
  • 97
  • 131
  • This does not mean "you can use the button tag, as long as it's isn't in an html form," it just means that if you use multiple buttons in an HTML form, internet explorer will not tell you which button was clicked, but will instead include the text of each button ( not the value ). For a single-button form this should act the exact same as an submit imput. – Robert Kuykendall Dec 14 '09 at 22:15
  • I don't understand how "internet explorer will submit the text" doesn't make button kinda unreliable, but I guess with these answers as a starting point I can test it out with the confidence that I'm not missing some broken aspect. – Kzqai Dec 14 '09 at 22:58
  • 1
    It concerns multiple '' it would send "Accept Terms." instead of correctly sending 'submit-accept'. Furthermore, it sends this for every button in the form, not just the one that was clicked, rendering the multiple-button feature useless. But you're not planning on using this feature, so you don't need to worry. – Robert Kuykendall Dec 15 '09 at 01:13
1

Here's a site that explains the differences: http://www.javascriptkit.com/howto/button.shtml

Basically, the input tag allows just text (although you can use a background image) while the button allows you to add images, tables, divs and whatever else. Also, it doesn't require it to be nested within a form tag.

Chris Haas
  • 47,821
  • 10
  • 127
  • 248
1

You might also run into these problems:

Another thing is related to styling it using the sliding-door technique: you need to insert another tag e.g. <span> to make it work.

Community
  • 1
  • 1
moey
  • 9,619
  • 20
  • 64
  • 109
0

as far as I am concerned the difference between submit and button tags is this: gives you the option to have different text displayed than the element's value

Let's say you have a list of products then next to each product you want a button to add it to the customer's cart:

product1 : <add to cart>
product2 : <add to cart>
product3 : <add to cart>

then you could do this:

<button name="buy" type="submit" value="product2"> add to cart </button>

Now the problem is that IE will send the form with value="add to cart" instead of value="product2"

The easiest way to workaroound this issue is by adding onclick="this.value='product2'"

So this:

<button name="buy" type="submit" value="product2" onclick="this.value='product2'"> add to cart </button>

will do the trick on all major browsers - I have actually used this on a form with multiple buttons and works with Chrome Firefox and IE

George Panic
  • 421
  • 1
  • 8
  • 19
-2

Looks like the main reason to use <button> is to allow for CSS markup of that button and the ability to style the button with images: (see here: http://www.javascriptkit.com/howto/button.shtml)

However, I think the more adopted approach I've seen in (X)HTML + CSS is to use a div and style it completely with images and :hover pseudo-classes (simulating button downpress... can't add more than one link per answer, so just google "div button" you'll see lots of examples of this), and using javascript to do form submission or AJAX call... this also makes even more sense if you don't use HTML forms, and do all submissions with AJAX.

r00fus
  • 2,196
  • 2
  • 14
  • 16
  • 2
    Second approach seems pretty-much unfriendly to progressive enhancement, right? – Kzqai Dec 14 '09 at 21:37
  • There's nothing you can do with a
    you can't do with a
    – Brad Koch Jun 29 '13 at 18:43