I saw that we can write:
<form method="link" action="foo.html" >
<input type="submit" />
</form>
To make a "link button".
But I know we can write:
<a href="foo.html" ><input type="button" /></a>
Which will do the same.
What's the difference? What's their browser compatibility?
All the mentioned methods achieve the same result, except if they contain multiple arguments, such as:
For reference I use:
For a button link
The second case will not handle addition input arguments inside A tag. It will follow href only while form will add all inputs to request GET string.
and
would work different
With a
<a name="markname"></a>
you can scroll the position of that tag into view by using the URL e.g.http://example.com/index.html#markname
. I don't think that either form or input do so. And I think that for using<input type="button" window.location.href='foo.html'/>
, JavaScript would have to be activated. Also,<form>s
usually cause a line break,<a>s
don't.Differences in functionality.
While yes, they both behave somewhat like one another, there's still some reasons to use elements for what they're meant for. For the most part, you lose functionality with a button link; you cannot right click and open in a new tab or window, for example.
Consider semantics and specs as well: The button element (whichever variation you use;
<input type="button">
or<button></button>
) was meant for use by forms.That page you link to is incorrect. There is no
link
value for themethod
attribute in HTML. This will cause the form to fall back to the default value for the method attribute,get
, which is equivalent to an anchor element with ahref
attribute anyway, as both will result in a HTTPGET
request. The only valid values of a form'smethod
in HTML5 are "get" and "post".This is the same as your example, but valid; and is equivalent to:
You should use semantics to determine which way to implement your form. Since there are no form fields for the user to fill in, this isn't really a form, and thus you need not use
<form>
to get the effect.An example of when to use a
GET
form is a search box:The above allows the visitor to input their own search query, whereas this anchor element does not:
Yet both will go to the same page when submitted/clicked (assuming the user doesn't change the text field in the first
As an aside, I use the following CSS to get links that look like buttons: