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>
jQuery is not going to solve this one OOTB. It can help, but none of
stopPropagation,stopImmediatePropagation,preventDefault,return falsewill 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
nulling out theonclickattribute 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.