Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable mouse and key events in Div

Tags:

html

jquery

I have multiple Html elements inside a div. Is there any way to disable mouse and key events for that Div(Not for individual elements)? The elements are auto generated and are inside multiple spans. Hence i just want to disable the whole div so that nothing can be changed but readonly.

like image 606
nebula Avatar asked May 22 '13 09:05

nebula


People also ask

How do I disable mouse events in CSS?

To disable the click event in HTML, the “pointer-events” property of CSS is used. For this purpose, add an HTML element and set the value of the pointer-events property as “none” to disable its click event.

How do you make a Div disabled in CSS?

An element can be disabled in HTML by setting disable property to true and enabled again by setting disabled=false. By using jQuery, you can grab the element you want to enable or disable and change this property by using the prop() or attr() function, depending upon the version of jQuery you are using.

What is Pointerevents?

Pointer events are DOM events that are fired for a pointing device. They are designed to create a single DOM event model to handle pointing input devices such as a mouse, pen/stylus or touch (such as one or more fingers). The pointer is a hardware-agnostic device that can target a specific set of screen coordinates.


2 Answers

Try with .unbind() like

$('#my_div_id').unbind();

it will remove all the events that will attached to that div.See this UNBIND

You can use .die also like

$('#my_div_id').die();
like image 54
Gautam3164 Avatar answered Sep 20 '22 13:09

Gautam3164


As per jQuery docs, unbind method removes a previously-attached event handler from the elements. My understanding is that you just want to prevent the default browser events from being triggered, instead of removing previously attached events.

There is a simple CSS solution for it, which doesn't work on IE10 and below:

#yourDiv {pointer-events:none;}

A solution using jQuery would be adding an event to your div, and then preventing default behavior from happening on events of your choice:

$('#yourDiv').click(function(e){
    e.preventDefault();    
});

Click events should also work for keyboard access to check boxes and radio buttons, but in case you need, for example, prevent text typing on an input field, just add keyboard events to the list:

$('#yourDiv').click(function(e){
    e.preventDefault();    
});
$('#yourDiv').keypress(function(e){
    e.preventDefault();
});

I am just posting this answer because it might help other people looking for similar functionality, but I believe there are better ways of doing this on your case. You could run a function to set your elements to disabled $('#yourDiv input').prop("disabled", true); after loading. Hard to tell without knowing your html.

like image 25
Hugo Silva Avatar answered Sep 16 '22 13:09

Hugo Silva