Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/HTML - Disable OnClick

as the title says I'm trying to disable OnClick for specific

<div id="test" style="display: block; opacity: 0.89;"></div>

this div is loaded from external script which is obfuscated so i cannot see the specific code.

What I have tried to remove this

$("#test").click(function(){ return false});
$("#test").unbind();
$("#test").removeAttr("onclick");
//Suggestions which do not work
$('#test').on('click', function(e) {e.preventDefault();return false;});
$("#test").click(function(event){
event.preventDefault();
});

None of the above work.

EDIT: Solved my problem using .unbind("click"); after the div was created.

like image 552
user2534466 Avatar asked Oct 11 '13 14:10

user2534466


People also ask

How to disable onclick jQuery?

unbind(); $("#test"). removeAttr("onclick"); //Suggestions which do not work $('#test'). on('click', function(e) {e. preventDefault();return false;}); $("#test").

How do I turn off click events?

To disable the click event on an element, we can use the pointer-events property. Simply set the pointer-events property to none to disable the click event on any element that you want. In the following example, we have disabled the click event for a button by simply setting its pointer-events property to none .

How do I turn off onclick event in react?

If we want to disable our button after clicking on it, We can disable it by using react's state. We will set the button's disable state to false on load and add the onClick function in our button element, which will set the button's disable state to true .


3 Answers

You can add off("click");

Fiddle

$(function () {
    $("#test").click(function(){alert('test');});
    $("#test").off('click');

});

this code will demonstrate removing a previously added click event.

like image 175
Smeegs Avatar answered Oct 13 '22 14:10

Smeegs


Simple form, combination jquery with javascript.

// To disable:    

$('#test').each(function (){
    this.style.pointerEvents = 'none'; 
}); 

// To re-enable:
$('#test').each(function (){
    this.style.pointerEvents = 'auto'; 
}); 

So, you can change the selector for multiple tags

like image 26
Jose María Vallejo Avatar answered Oct 13 '22 14:10

Jose María Vallejo


None of the options you've tried would work if you use them before the div has been added to the page. So be sure to put your code somewhere after the other script that creates the div.

If more than one element had that same id that would also be a problem.

Regarding the individual methods you've tried:

$("#test").click(function(){ return false});
$("#test").unbind();
$("#test").removeAttr("onclick");

The first line will add a new click handler to the element that prevents the default behaviour and stops the event propagating up, but it won't stop other event handlers bound to the element from running - especially if they run before your new handler, obviously.

The second line will remove event handlers that were attached with jQuery, but not handlers attached by other means.

The third line should work to remove an inline onclick attribute if it exists, but not handlers added with jQuery.

Assuming you still can't stop the click behaviour even after ensuring your code runs after the div is added to the page, you could try something like the following:

var $test = $("#test").removeAttr("onclick"),
    $contents = $test.contents().detach();
$test.replaceWith($test.clone(false).append($contents));

Demo: http://jsfiddle.net/A9tmu/3/

The idea there is to replace the element with a clone of itself, because the cloned version (when created with .clone(false)) will not retain event handlers. I'm temporarily detaching the divs contents before cloning so that any child elements will get to keep their event handlers.

like image 6
nnnnnn Avatar answered Oct 13 '22 14:10

nnnnnn