Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable and then enable onclick event on <div> with javascript [closed]

Following is the code which i am trying

document.getElementById("id").disabled = true; 
like image 455
user2798259 Avatar asked Sep 24 '13 13:09

user2798259


People also ask

Can we apply onclick event on div?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.

How do I enable and disable a div?

If you want to disable all the div's controls, you can try adding a transparent div on the div to disable, you gonna make it unclickable, also use fadeTo to create a disable appearance. try this. This doesn't prevent the user going to the links or inputs with the "tab" key where they can still interact with them.


2 Answers

You can use the CSS property pointer-events to disable the click event on any element:

https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events

// To disable:     document.getElementById('id').style.pointerEvents = 'none'; // To re-enable: document.getElementById('id').style.pointerEvents = 'auto';  // Use '' if you want to allow CSS rules to set the value 

Here is a JsBin: http://jsbin.com/oyAhuRI/1/edit

like image 71
Tibos Avatar answered Sep 28 '22 05:09

Tibos


I'm confused by your question, seems to me that the question title and body are asking different things. If you want to disable/enable a click event on a div simply do:

$("#id").on('click', function(){ //enables click event     //do your thing here });  $("#id").off('click'); //disables click event 

If you want to disable a div, use the following code:

$("#id").attr('disabled','disabled'); 

Hope this helps.

edit: oops, didn't see the other bind/unbind answer. Sorry. Those methods are also correct, though they've been deprecated in jQuery 1.7, and replaced by on()/off()

like image 21
Paraphiliac Ostrich Avatar answered Sep 28 '22 07:09

Paraphiliac Ostrich