Reputation: 546
I just want to enable / disable onclick
and href on elements (a or div).
I don't know how to do this.
I can disable onclick
by adding an handler on click event, but the href is still available.
$(this).unbind().click(function(event){
event.preventDefault();
return;
});
Edit FOUND A HACK FOR A ELEMENTS
if ($(this).attr("href")) {
$(this).attr("x-href", $(this).attr("href"));
$(this).removeAttr("href");
}
Upvotes: 5
Views: 24964
Reputation: 1219
If you return false
on the onclick event, the href
is irgnored.
This will go to Goole: <a
href="http://www.google.com"
onclick="alert('Go to
Google')">Test</a>
This will not go to Google: <a href="http://www.google.com" onclick="alert('Go to Google'); return false;">Test</a>
Upvotes: 6
Reputation: 546
Ok i've found a workaround : putting an overlay over the main div containing all the elements i wanted to disable .. It just works.
Upvotes: 5
Reputation: 253318
You could try the following:
$('a, div').click(
function(e){
return false;
// cancels default action *and* stops propagation
// or e.preventDefault;
// cancels default action without stopping propagation
});
MDC documentation for preventDefault
, jQuery documentation for event.preventDefault
.
SO question: JavaScript event.preventDefault
vs return false
.
I'm unsure as to the problem of the "href
still being available," since the click
event is cancelled; however if you want to remove the href
from a
elements:
$('a[href]').attr('href','#');
will remove them (or, rather, replace the URL with a #
).
Edited in response to comment (to question) by OP:
Ok, sorry ;) I just want to be able (by clicking on a button), to disable / enable all the links (click or href) over elements (div or a)
$('#buttonRemoveClickId, .buttonClassName').click(
function() {
$('a, div').unbind('click');
});
$('#buttonReplaceClickId, .buttonOtherClassName').click(
function() {
$('a, div').bind('click');
});
Upvotes: 2
Reputation: 93664
You can set the href
attribute directly to ""
to prevent the original from showing up in the status bar, if that's what you're asking.
$(this).unbind().click(function(event){
event.preventDefault();
}).attr("href", "");
Otherwise, a event.preventDefault()
already stops links from being clickable.
Upvotes: 0