This is a preference and a bit more.
Here is a rule of thumb:
- For navigation, just use anchor to style it and let it use the
href attribute well. - For quick actions (play, pause, stop, + 1) just use the button . doesn't have
href for any reason!
Consider this code.
const anchor = document.getElementsByTagName('a')[0] const button = document.getElementsByTagName('button')[0] anchor.addEventListener('click', e => { console.log('anchor clicked') e.preventDefault() }, false) button.addEventListener('click', e => { console.log('button clicked') })
a { text-decoration: none; padding: 2px; } button { background: none; border: none; font-size: 16px; } .btn { background-color: blue; color: white; display: inline-block; text-align: center; }
<a class="btn" href="#"> Anchor </a> <hr/> <button class="btn" type="button"> Button </button>
Styling
Both of them look almost the same (with minimal justification), the only problem is that you have to undo some styles that come with the button as border and background , but with a snap you won’t get the click that you get with the button.
Functionality
But since binding <a> requires <a href="some/path"> to work, even if it's just # , if you don't need to navigate after clicking on the anchor, you will have to use e.preventDefault() in your javascript to prevent his.
I think there is more to it than just styling and functionality.
It’s just a programmer to think about them, and it’s best to pay attention to details, since button needs a special type attribute so that it works correctly Always and needs bindings e.preventDefault() .
source share