Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a <button> element to link to a location without wrapping it in an <a href ... tag?

Just wondering if there is a way to get a HTML <button> element to link to a location without wrapping it in an <a href... tag?

Button currently looks like:

<button>Visit Page Now</button> 

What I would prefer not to have:

<a href="link.html"><button>Visit Page Now</button></a> 

The button is not being used within a form so <input type="button"> is not an option. I am just curious to see if there is a way to link this particular element without needing to wrap it in an <a href tag.

Looking forward to hearing some options/opinions.

like image 781
Dan Avatar asked Feb 17 '11 06:02

Dan


People also ask

Can button element have href attribute?

HTML buttons cannot have href attribute if they are created using button <button> </button> HTML tags. However, you can use href attribute if you create the buttons using link <a> </a> HTML tags.

How do you hyperlink a button in HTML?

The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute. If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit"> .

Can you have a tag without href?

Yes, it is valid to use the anchor tag without a href attribute. If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.

How do I make a button work like a link?

Adding styles as button to a link: This method create a simple anchor tag link and then apply some CSS property to makes it like a button. Using form tags: This method uses form tag and button tag. When button is clicked then the form action attribute is called and web page redirect into the given location.


1 Answers

Inline Javascript:

<button onclick="window.location='http://www.example.com';">Visit Page Now</button> 

Defining a function in Javascript:

<script>     function visitPage(){         window.location='http://www.example.com';     } </script> <button onclick="visitPage();">Visit Page Now</button> 

or in Jquery

<button id="some_id">Visit Page Now</button>  $('#some_id').click(function() {   window.location='http://www.example.com'; }); 
like image 126
aiham Avatar answered Sep 19 '22 15:09

aiham