I'm using an a tag but with an onclick and I don't know if I'm doing it right or actually the right way is using an href without me needing it.
I'm using an a tag but with an onclick and I don't know if I'm doing it right or actually the right way is using an href without me needing it.
If you check the specification right here you will notice how:
An element that is of type
a
has the possibility of including a type attributehref
which serves to link to a URL as an external resource that must be consumed when clicking on the text that you place between the opening and closing of said tag; more or less so:So it is not so much that what you do to give it an action in the event is incorrect,
click
but rather that it is other, since if we include said attribute we obtain the same functionalityConsider that if you are going to use this event, it would be more convenient or logical for situations in which, for example, you try to manipulate the data that is entered into an event
input
and therefore you will perform one or more actions accordingly.Specifically if we look at this standard you can read and I quote:
So to the question, it is not necessary, but using JS to give this effect is adding, I consider, extra complexity that with a simple attribute that exists natively you will be able to achieve.
Hello, the anchor tag should only be used for navigation using a suitable URL, these tags are frequently abused with the use of onclick events to create pseudo-buttons by adjusting href="#".
These values can lead to unexpected behavior.
I would recommend that you use the tag instead
<button>
.If you want more information on this subject, I leave you this Mozilla link .