Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's good practice for <button>? Can it replace <a> or is it only meant for forms?

What's good practice for <button>?

Can it replace <a> tag or is it only meant for forms?

like image 260
SIr Codealot Avatar asked Feb 22 '23 10:02

SIr Codealot


1 Answers

A link links to something - at least it should! It is semantically wrong to do the following:

<a href="#" onclick="doSomething(); return false;">Do something</a>

This should be replaced with a button, as it is there for that purpose - it works as a trigger for something the user (programmer) specifies; the purpose of a <button type="button"> element is thus not clear. In contrast, the purpose of a link is very clear - it should point somewhere!

As HTML is a markup language, it does not matter all that much what you do, if you do not think SEO. You can achieve the same thing with a <a>tag as you can with a <button> tag, like a <span> can act exactly as a <div> - semantically though, it is incorrect.

like image 131
Frederik Wordenskjold Avatar answered May 14 '23 16:05

Frederik Wordenskjold