HTML <a> tag wants to add both href and onclick working

I would like to ask about the HTML tag

<a href="www.mysite.com" onClick="javascript.function();">Item</a> 

How to make this a tag work with href and onClick ? (first select onClick then href)

+81
javascript html href tags onclick
Feb 14 '13 at 3:56
source share
2 answers

You already have what you need, with a slight syntax change:

 <a href="www.mysite.com" onclick="return theFunction();">Item</a> <script type="text/javascript"> function theFunction () { // return true or false, depending on whether you want to allow the `href` property to follow through or not } </script> 

By default, the <a> onclick and href tag properties should execute onclick by default and then follow href until onclick returns false , canceling the event (or the event has not been prevented)

+153
Feb 14 '13 at 4:01
source share

Use [jQuery] [1]. You need to capture the click event and then go to the website.

 $("#myHref").on('click', function() { alert("inside onclick"); window.location = "http://www.google.com"; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="myHref">Click me</a> 
+9
Feb 14 '13 at 4:01
source share



All Articles