When I want to prevent default behaviour of anchor tag I`m using
<a href="javascript:void(0);">link</a>
Which is the most effective solution ?
Yes, disabled isn't supported attribute by the anchor tab, but the CSS attribute selector does find it and so does jQuery's.
An example of a gracefully degrading solution:
<a href="no-script.html" id="myLink">link</a>
<script>
document.getElementById("myLink").onclick = function() {
// do things, and then
return false;
};
</script>
Demo: http://jsfiddle.net/karim79/PkgWL/1/
This is a nice approach, if you're using jquery you can also do:
<a id="link" href="javascript:void(0)">link</a>
<script type="text/javascript">
$("#link").click(function(ev) {
ev.preventDefault();
});
</script>
preventDefault can be useful also to prevent submitting form
You can also have this:
<a href="#" onclick="return false;">link</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With