Reputation: 57916
I have the below which disables all hyperlinks but after an event I want to enable them back all again, how can I do this?
$("a").click(function() { return false; });
I don't think its as simple as just setting it to true. ;)
Thanks all
Upvotes: 7
Views: 18872
Reputation: 82
This replace all links on page to #
which means: disable links for other pages
$(document).ready(function() {
$('a').attr('href', '#');
});
Upvotes: 4
Reputation: 3752
Another way, if you strictly want the url to be inaccessible as well, you can remove href
attribute from a
tag. Something like that:
$('a').removeAttr('href');
additionaly changing color of text and onhover cursor:
$('a').css('color', '#333');
$('a').hover(function () {
$(this).css('cursor', 'not-allowed');
);
Upvotes: 0
Reputation: 1806
Or even better, simply toggle the display
of a large element with the high z-index
. In the example below, the div covers the body.
var link = document.getElementsByTagName('a')[2];
var noclick = document.getElementById('noclick');
link.onclick = function() {
noclick.className = 'on';
}
#noclick {
display: none;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
/* Make sure this floats over content */
z-index: 100;
}
#noclick.on {
display: block;
}
<body>
<div id="noclick"></div>
<a href="#">one</a>
<a href="#">two</a>
<a href="#">three</a>
</body>
Upvotes: 0
Reputation: 413702
Instead of binding your "click" handler that way, do this:
$('a').bind("click.myDisable", function() { return false; });
Then when you want to remove that handler it's easy:
$('a').unbind("click.myDisable");
That way you avoid messing up other stuff that might be bound to "click". If you just unbind "click", you unbind everything bound to that event.
edit in 2014 — the way you bind events now is with .on()
:
$('a').on('click.myDisable', function() { return false; });
Probably it'd be better to do this:
$('a').on('click.myDisable', function(e) { e.preventDefault(); });
To unbind:
$('a').off('click.myDisable');
Finally, you could bind a handler to the document body and deal with <a>
tags that are dynamically added:
$('body').on('click.myDisable', 'a', function(e) { e.preventDefault(); });
// to unbind
$('body').off('click.myDisable');
Upvotes: 20
Reputation: 322452
Binding and unbinding takes some overhead.
A different solution would be to add a class like disabled
, then use hasClass('disabled')
to test and see whether or not it should return false
.
$('a').addClass('disabled');
$('a').click(function() {
if($(this).hasClass('disabled'))
return false;
});
Upvotes: 5
Reputation: 14967
$(function(){
$.myStopAnchor = function(){
$stop = true;
}
$.myNoStopAnchor = function(){
$stop = false;
}
$.myNoStopAnchor();
$("a").click(function(ev) {
if ($stop){
ev.stopPropagation()
}
return !$stop;
});
});
Upvotes: 0
Reputation: 25249
Try this:
// Better to use the live event handler here, performancewise
$('a').live('click', function() {... return false;});
// Now simply kill the binding like this
$('a').die('click');
bye
Upvotes: 6
Reputation: 60413
var disableLink = function(){ return false;};
$('a').bind('click', disableLink);
to restore:
$('a').unbind('click', dsiableLink);
Upvotes: 4