0

How can I disable my javascript onclick button event on another div that is located inside a main div which contains my id. Here is the code:

 <div id="clickme">

 <div id="disabled_clickme">hello</div>

 </div>

 $('#clickme').click(function () {
 //SOME CODE
 }

How can I disable my clickme function?

3 Answers 3

1

You can use .off()

$("#clickme").off();

http://api.jquery.com/off/ -- Calling .off() with no args removes all event handlers for the specified selector.

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

Comments

0

In JavaScript, the click event is triggered in the child nodes and then propagated to their parents. This is called event bubbling.

You can use the stopPropagation method in the event object from #disabled_clickme click to prevent it from reaching #clickme.

$('#disabled_clickme').click(function(e) {
    e.stopPropagation();
});

Live example: http://jsfiddle.net/PUTTQ/

1 Comment

Thanks, now I just need to find the stopPropagation for function
0
$('#clickme').click(function () {
    // SOME CODES
});

// disables click event
$('#disabled_clickme').click(function(){return false;});

The result of this is that if you click the <div id="disabled_clickme"> it will not return any codes. BUT if you click the <div id="clickme"> the codes for that will still work.

Comments

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.