How can I disable HREF if onclick is executed? How can I disable HREF if onclick is executed? javascript javascript

How can I disable HREF if onclick is executed?


You can use the first un-edited solution, if you put return first in the onclick attribute:

<a href="https://example.com/no-js-login" onclick="return yes_js_login();">Log in</a>yes_js_login = function() {     // Your code here     return false;}

Example: https://jsfiddle.net/FXkgV/289/


    yes_js_login = function() {         // Your code here         return false;    }

If you return false it should prevent the default action (going to the href).

Edit: Sorry that doesn't seem to work, you can do the following instead:

<a href="http://example.com/no-js-login" onclick="yes_js_login(); return false;">Link</a>


Simply disable default browser behaviour using preventDefault and pass the event within your HTML.

<a href=/foo onclick= yes_js_login(event)>Lorem ipsum</a>

yes_js_login = function(e) {  e.preventDefault();}