5

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.

4
  • 2
    a div has no default behavior for onclick, so what are you trying to prevent? Is there more code maybe? Commented Oct 11, 2013 at 14:08
  • Is it the only element on the page with that id? And are you trying the above jQuery code after the element is added to the page? Commented Oct 11, 2013 at 14:15
  • Thank you nnnnnn, the problem was that i was using my code before the div was created, now i have added .unbind("click"); after its loaded and it works fine :) Commented Oct 11, 2013 at 14:18
  • Cool. Since that worked I've added it as an answer. Commented Oct 11, 2013 at 14:27

8 Answers 8

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

is there an 'on' equivalent to 'off'. In that you may want to re-enable a click event, previously turned 'off'.
8

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

2 Comments

doesn't this solution disable mouse scrolling on element?
Seems like the only solution keeping old onclick content, so you don't need to repeat it again!
6

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.

2 Comments

How would you enable them after disabling it?
@Robert re-enabling a click handler from an external script is an even more fiddly use case than trying to disable it in the first place. You may not be able to if you don't know how the original one was bound. If it was an onclick you could save a reference to the original before removing it. If the original used addEventListener() I don't think you can get a reference to the existing handler.
1

Try this:

To disable:

$("#test").css("pointerEvents","none");

To enable:

$("#test").css("pointerEvents","auto");

Comments

0
$('#test').on('click', function(e) {e.preventDefault();return false;});

1 Comment

Make sure you include the code above AFTER <div id="test"/> is included into DOM. Otherwise use this: $(document).on('click', '#test', function(e) {e.preventDefault();return false;});
0

try this:

$("#test").click(function(event) {
    event.preventDefault();
});

2 Comments

return false; will work with simple javascript <a href="http://websiteURL/" onclick="javascript:return false;">TEST</a>
return false also works from jQuery event handlers, as explained in the documentation.
0

Try this?

$('#test').unbind('click')

OR

   $( "#test" ).bind( "click", handler );
   $( "#test" ).unbind( "click", handler );

Try this

$( "#test" ).on("click", function () {
  });
$("#test").off("click");

1 Comment

@user2534466 Updated my answer following this: stackoverflow.com/questions/209029/…
0

You can unbind click event into click event:

 $("#test").click(function(){
        $( "#test" ).unbind( "click");
        alert('Jquery/Javascript Event');
    });

Try JsFiddle

1 Comment

What is handler?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.