22 Votes

HTML: Use Button as Link

Question by Guest | Last update on 2021-07-06 | Created on 2014-09-18

I would like to use a button (<input type="button">) as a link (<a href=""></a>) on my website to call the page "download.php?id=1" with it.

Is it possible to repurpose a normal button in a way that it can be used and behaves like a usual hyperlink?

ReplyPositiveNegativeDateVotes
11 Vote

You can simply assign an OnClick-Event to your button to call the link with it. 

For example in the following way:

<input type="button" value="Download" 
   onclick="window.location='download.php?id=1';"> 

If you are already using jQuery on your page, of course, it is more elegant to set an appropriate jQuery-event-listener.

However, both methods are only working, if JavaScript is activated.
Last update on 2021-07-06 | Created on 2014-09-18

ReplyPositive Negative
11 Vote

Another possibility is to build an invisible form around the button.

As "action", you can just use your link, as "method" you can use "get" and the button type must be "submit".

<form action="download.php?id=1" method="get">
  <input type="submit" value="Download">
</form>

With this way, it would also work if JavaScript is not activated.
Last update on 2021-07-06 | Created on 2014-09-18

ReplyPositive Negative
00 Votes

In most browsers, it is also working by just enclosing the button with an a-tag to make it a "link".

<a href="download.php?id=1">
   <input type="button" value="Download"> 
</a>

However, this is not valid HTML.

Alternatively, you can build your own button using CSS:

.btlink {
  border: 1px solid #333;
  background-color: #EEE;
  padding: 4px 6px 4px 6px;
  display: block;
  text-decoration: none; 
}

And here is how to apply it:

<a href="download.php?id=1" class="btlink">Download</a>

Of course, you can change this CSS to your own needs depending on you would like to style your button. For example like all of the other buttons on your website.
2014-09-18 at 23:46

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.