How to include both href and onclick to HTML?

Spread the love

Sometimes, we want to include both href and onclick to HTML.

In this article, we’ll look at how to include both href and onclick to HTML.

How to include both href and onclick to HTML?

To include both href and onclick to HTML, we add the click listener to it.

For instance, we write

<a id="myButton" href="http://google.com">Click me!</a>

to add a link.

Then we write

document.querySelector("#myButton").addEventListener("click", (e) => {
  console.log("Clicked!");
});

to select the link with querySelector.

Then we call addEventListener to add a click listener to it.

Conclusion

To include both href and onclick to HTML, we add the click listener to it.

Leave a Reply

Your email address will not be published.