Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 858
user2129794 Avatar asked Dec 11 '22 07:12

user2129794


1 Answers

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>
like image 92
Esko Avatar answered Dec 30 '22 19:12

Esko