I would like to disable onclick
event with CSS. Possible?
Let's say I have
<div id="btnCopy" class="button" onclick="btnCopy(this);"><img src="copy.png"></div>
and by adding class "disabled"
document.getElementById("btnCopy").className += " disabled";
I would like to turn off onclick event for this element, so onclick="btnCopy(this);"
would not be active.
And by removing "disabled" class
document.getElementById("btnCopy").className = document.getElementById("btnCopy").className.replace(/(?:^|\s)disabled(?!\S)/, '');
it would go back to normal, so onclick
event will be active.
Conclusion. To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none . Also, we can add a click event listener to the div and then call event. preventDefault inside.
In jQuery, you can create a button event for example when a button is clicked an alert message will show in the display. Similarly, you can disable this functionality by using jQuery. To show, the message jQuery provides the on() method and to disable this event you will get the off() method.
You can simply use the CSS pointer-events property to disable a link. The none value of this property specify the element is never the target of pointer events.
CSS Styling for Disabling an HTML ButtonWe can set a couple of style properties on a button to effectively make it disabled without using the disabled attribute. The most basic property is pointer-events: none; . This makes the element unable to register clicks or hovers.
You can do this.
pointer-events: none; cursor: default;
more here
(Note that this is not accessible; it does not disable tabbing to the element and pressing "Enter"; the event will still fire in this case.)
In some browsers you can set pointer-events: none
, but that disables all mouse events not just clicks. See: https://developer.mozilla.org/en/CSS/pointer-events/
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