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 off() method is used to remove event handler from the element attached with the on() method. Use off() method after click event is triggered to disable element for the further click. $('#clickElement'). off('click');
Bind both handlers with the . click() function and whichever one you bind first can call stopImmediatePropogation() to prevent the second one from running. (Or combine them into a single handler.)
preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form. Prevent a link from following the URL.
To disable clicks with JavaScript, we can write: const foo = document. querySelector('#foo') foo. addEventListener('click', (event) => { event.
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 the onclick
attribute for disabled anchors):
Here's what I mean:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <title>Disable clicks</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> </head> <body> <a href="#" onclick="alert('panic!')">Let's panic</a> <a href="#" onclick="alert('panic!')" disabled="disabled">I can't panic no more</a> <script> $('a[onclick]').each(function(){ $(this).data('onclick', this.onclick); this.onclick = function(event) { if($(this).attr('disabled')) { // HERE return false; }; $(this).data('onclick').call(this, event || window.event); }; }); </script> </body> </html>
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 (with return false
).
The benefit there is that to enable an anchor again you simply .removeAttr('disabled')
on it.
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