there are some links with onclick event actions
<a href="#" onclick="alert('panic!')">Let's panic</a>
<a href="#" onclick="alert('panic!')" disabled="disabled">I can't panic no more</a>
I need prevent event actons execution on links with disabled attribute without removing onclick actions.
$('a[disabled]').click(function(e){
e.stopPropagation();
return false;
});
This code doesn't helps me.
update Even this code doesn't work
<html><head><script type='text/javascript' src='jquery-1.3.2.js'></script></head>
<body>
<a href='#' onclick="alert('HA-ha!')" disabled="disabled" class="disabled">TEST</a>
<script type="text/javascript">
$('a[disabled], a.disabled').click(function(e){
console.log('override?');
e.stopImmediatePropagation();
e.preventDefault();
e.stopPropagation();
return false;
});
</script>
</body></html>
Any click handlers added by jQuery seem to fire after those added in the mark up. My solution would be to apply the click handlers using jQuery instead of in the mark up, but you may not have enough control over the code to do this. If you do, then simply don't apply the click handler to anchor tags with class disabled (and, yes, I would use a class rather than an inappropriate attribute). If you don't have control over the mark up, then you might want to replace the click handler using jQuery, storing it for later reapplication.
Tested with:
I found a pretty simple solution to preventing onclicks today, but keeping the action if you need it.
I'm not a huge fan of using "javascript:", but this was by far the simplest solution to the problem.
Hope it helps!
disabled
isn't a property of anchors. Use something likerel='disabled'
instead.Update:
Ah, of course. It still fires the
alert
because it's actually inline in the markup! I never do that and so didn't notice. Take that click handler out of the markup and do it in jQuery in the first place and so you can then just do something like:jQuery is not going to solve this one OOTB. It can help, but none of
stopPropagation
,stopImmediatePropagation
,preventDefault
,return false
will work if you simply attach them to the element. You need to override the element's click handler.However you state in your question "without removing onclick actions". So you need to override the default behavior at the point the event is triggered, (as opposed to the cleaner approach of simply
null
ing out theonclick
attribute for disabled anchors):Here's what I mean:
Demo here.
The approach there is to override the inline click handler (
onclick
) with preemptive logic to catch the case where the anchor is "disabled" and then cancel the event (withreturn false
).The benefit there is that to enable an anchor again you simply
.removeAttr('disabled')
on it.The problem is that jQuery adds events in order. To stop other events, the events you need to stop must come after your stopping code. Since you have code in your on click, you will need to change up the order. This is what I would do:
The benefit is just remove/add the disabled class using jQuery:
$('a#whatever').addClass('disabled')
or remove it$('a#whatever').removeClass('disabled')
and no other cleanup/setup is required.