How can I disable HREF if onclick is executed?
Solution 1
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/
Solution 2
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>
Solution 3
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();
}
Solution 4
<a href="http://www.google.com" class="ignore-click">Test</a>
with jQuery:
<script>
$(".ignore-click").click(function(){
return false;
})
</script>
with JavaScript
<script>
for (var i = 0; i < document.getElementsByClassName("ignore-click").length; i++) {
document.getElementsByClassName("ignore-click")[i].addEventListener('click', function (event) {
event.preventDefault();
return false;
});
}
</script>
You assign class .ignore-click
to as many elements you like and clicks on those elements will be ignored
Solution 5
You can use this simple code:
<a href="" onclick="return false;">add new action</a><br>
Related videos on Youtube
Ky -
Any pronouns. Really, as long as you're respectful, use any singular third-person pronouns to refer to me. More about me on my site! https://KyLeggiero.me Starting on 2016-11-21 and going forward in perpetuity, all content I post on a website in the StackExchange network is written specially for that post, and is licensed under BH-0-PD unless otherwise stated.
Updated on July 08, 2022Comments
-
Ky - almost 2 years
I have an anchor with both
HREF
andONCLICK
attributes set. If clicked and Javascript is enabled, I want it to only executeONCLICK
and ignoreHREF
. Likewise, if Javascript is disabled or unsupported, I want it to follow theHREF
URL and ignoreONCLICK
. Below is an example of what I'm doing, which would execute the JS and follow the link concurrently (usually the JS is executed and then the page changes):<A HREF="http://example.com/no-js-login" ONCLICK="yes_js_login()">Log in</A>
what's the best way to do this?
I'm hoping for a Javascript answer, but I'll accept any method as long as it works, especially if this can be done with PHP. I've read "a href link executes and redirects page before javascript onclick function is able to finish" already, but it only delays
HREF
, but doesn't completely disable it. I'm also looking for something much simpler.-
ewein about 11 yearsI would use to anchors one with an href and one without. On page load check if javascript is enabled, if it is show the correct anchor else show the other.
-
Ky - over 8 years@ewein Why? That sounds like an awful lot of markup for a simple feature.
-
-
Itai Bar-Haim over 10 yearsThe edited answer does not answer the question, as it removes the actual link.
-
Uwe Kleine-König almost 10 yearsalternatively use
onclick="return yes_js_login();"
withyes_js_login
returningfalse
-
Ky - over 9 yearswhy are there two? Why are you putting JS in the
HREF
? -
Bo Johanson over 9 yearsSupuhstar, The actual code is dynamic, javascript, and will generate an <a> element for several hundred items that are defined externally. Each item will either specify a static link address, and thats where the generated <a> element should use the regular "href behaviour". Or, a javascript function call is specified for the item, in which case the <a> element's behaviour is calculated, based on the element's context. And in the code for the <a> generator, I just wanted to copy whatever is specified for each element, no testing whether it's a link address or a javascript funtion call.
-
Ky - over 9 yearsI don't understand how this answers my question. Who said I wanted several hundred links? I really don't understand the point of all this, or even half of what you just said.
-
Milan Simek over 8 yearsThat doesn't work for me. I have to use e.preventDefault()
-
Gabe Rogan almost 7 years@cgogolin see my answer.
-
didxga almost 7 yearsfor anyone who still stuck, may adding this line
event.stopImmediatePropagation()
-
Ky - over 6 yearsOver the past couple years I've changed my mind; this is a better solution
-
Matthew Mathieson about 6 yearsThis is a great solution! I used it for buttons that I have that when clicked a pseudo class
.btn-loading
is added to the clicked link, then if successful a checkmark(on error an X) replaces the loading animation. I don't want the button to be able to be clicked again in some instances(for both success and error), using this I can simply$(elemnent).addClass('disabled')
and easily achieve the functionality I was looking in an elegant way! -
Paras Sachapara about 6 yearsreturn false did not work in polymer... above solution did work... Thanks
-
DrunkDevKek almost 6 yearsHow wholesome !
-
Coder over 4 yearsBut this is not working with JSX react. Will this work?
-
Mark Lozano about 4 yearsthis saved a lot of time.
-
Ky - over 3 yearsThank you for chipping in. This doesn't help the scenario that I posed in my original question: "If clicked and Javascript is enabled, I want it to only execute
ONCLICK
and ignoreHREF
. Likewise, if Javascript is disabled or unsupported, I want it to follow theHREF
URL and ignoreONCLICK
." -
Duc Manh Nguyen about 3 yearsI don't know why my code does not work? var modal_button = document.querySelectorAll('[href="#bookingform"]'); if(modal_button.length){ for (var i = 0; i < modal_button.length; i++) { modal_button[i].addEventListener('click',function(e){ // some code e.preventDefault(); return false; }); } }