How to add href and onclick to an HTML anchor link?

Spread the love

Sometimes, we want to add href and onclick to an HTML anchor link.

In this article, we’ll look at how to add href and onclick to an HTML anchor link.

How to add href and onclick to an HTML anchor link?

To add href and onclick to an HTML anchor link, we return true in the click event handler.

For instance, we write

<a href="#Foo" onclick="return myFunction();">Do it</a>

to call myFunction on click and return its result.

Then we write

function myFunction() {
  //code
  return true;
}

to define myFunction, and we return true on click.

Conclusion

To add href and onclick to an HTML anchor link, we return true in the click event handler.

Leave a Reply

Your email address will not be published. Required fields are marked *