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/
Disable HREF by onclick inside
The click still bubbles upwards. Use event.stopPropagation();
instead. You are using event.preventDefault()
on an element that has no default click behaviour, so it does nothing.
$(function() { $(".inside > .fa").click(function(e) { e.stopPropagation(); console.log(e.target); });});
a { display: block; /* required otherwise you cannot have block level elements like div inside a */}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><a href='https://google.com' target="_top"> <div class='button'> <div class='inside'> <i class="fa fa-globe"></i> </div> </div></a>
How do I disable a href link in JavaScript?
Try this when you dont want user to redirect on click
<a href="javascript: void(0)">I am a useless link</a>
disable href after 1 click on html?
Pure javascript solution:
<script>
function clickAndDisable(link) {
// disable subsequent clicks
link.onclick = function(event) {
event.preventDefault();
}
}
</script>
<a href="target.html" onclick="clickAndDisable(this);">Click here</a>
Blazor link - disable href if there's an onclick method
The way to do it after release 3.1 of ASP.NET Core seems to be
<a href="" @onclick="@SomeAction" @onclick:preventDefault />
Disabled href tag
There is no disabled attribute for hyperlinks. If you don't want something to be linked then you'll need to remove the <a>
tag altogether.
Alternatively you can remove its href
attribute - though this has other UX and Accessibility issues as noted in the comments below so is not recommended.
Stop href link after onClick function is called
It looks like your script has an error and is not reaching the return block. If you add the try catch, it will ignore the errors in the script.
function loadPage(page){
try{
$("#content").html("Loading...");
$.post("page.php", {p: page}, function(data){
$("#content").html(data);
});
} catch(err){}
}
Related Topics
JavaScript Removeeventlistener Not Working
Why Does JavaScript Object Show Different Values in Console in Chrome, Firefox, Safari
How to Fix Navigator/Window/Document Is Undefined in Nuxt
How Does _Proto_ Differ from Constructor.Prototype
Filter Array of Objects with Another Array of Objects
Dynamic Keys for Object Literals in JavaScript
Access Iframe Elements in JavaScript
Fetch Request to Local File Not Working
JavaScript "Addeventlistener" Event Fires on Page Load
How Might I Find the Largest Number Contained in a JavaScript Array
How to Get the Mouse Position Without Events (Without Moving the Mouse)
Can JavaScript Connect with MySQL
How to Be Notified When an Element Is Added to the Page