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.
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.
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"> .
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.
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.
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'; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With