Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable onclick with CSS :: Possible?

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.

like image 425
Ωmega Avatar asked Jul 12 '12 12:07

Ωmega


People also ask

How do you stop a click event in CSS?

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.

How do I disable click event?

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.

Can CSS disable links?

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.

Can you disable a button with CSS?

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.


2 Answers

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.)

like image 73
user2591988 Avatar answered Oct 04 '22 01:10

user2591988


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/

like image 23
Richard M Avatar answered Oct 03 '22 23:10

Richard M