How to execute onClick and href both in an anchor tag ()

I have a link as follows:

<a href="www.google.com" onClick="someFunction()">Click me</a>

On clicking the link, it’s executing the JavaScript function, but it is not taking me to the href link.

How can I achieve the same?

Answer

I think the easiest way to do it is to make your function return true, so that after the function is complete, the anchor tag will behave like it normally does. This way you can also use the target-attribute if you want to use new window:

function myFunction() {
  alert("hello");
  return true;
}
<a href='http://www.google.com' onclick="myFunction();" target="google">Click me</a>

Leave a Reply

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