0

In $.ready() I have declared a click function as shown below

$(document).ready(function(){
    $("#btnClk").click(function(clkevent){
        //Doing Something Here
        clkevent.preventDefault();
    });

});

I want to remove this click() from $("#btnClk") in another javaScript function. How can I do this?

4
  • All of the information you require is at api.jquery.com/off Commented May 10, 2013 at 7:04
  • Small suggestion to google before posting here dude. Commented May 10, 2013 at 7:16
  • @VeeKayBee I searched google. Ended up with unbind function. But i wanted to remove only one or two functions. Commented May 10, 2013 at 7:36
  • @SubinJacob just to inform you. People are more expecting complex issues. Some guys may down vote you Commented May 10, 2013 at 9:31

5 Answers 5

1

One of the problems with the proposed solutions is they remove all click event handlers registered, which may not be desired.

A solution to this is to separate the handler method out to a common scope shared by the both the participating methods then use that method reference along with .off() to resolve this

function btnclickHandler(clkevent){
    //Doing Something Here
    clkevent.preventDefault();
}

$(document).ready(function(){
    $("#btnClk").click(btnclickHandler);
});

$("#btnClk").off('click', btnclickHandler);
Sign up to request clarification or add additional context in comments.

2 Comments

The off() method removes event handlers that were attached with .on().-- from api.jQuery.com. Here not attached with on(), any probs?
@SubinJacob it won't cause a problem since .bind() is currently (almost) synonymous with .on().
1

Old School

 $("#btnClk").unbind('click');

New School

 $("#btnClk").off('click');

1 Comment

This will remove all handlers if there were others added. If that's not desirable, you have to pass the handler you want removed, or use event namespaces
1

To register a click handler you should be using .on('click', ...) instead of .click - the latter can cause confusion because the same function is also used to trigger a click event.

To unregister the handler, use .off('click')

Please see the caveats at http://api.jquery.com/off/ regarding function handlers, name spaces, etc (my emphasis):

If a simple event name such as "click" is provided, all events of that type (both direct and delegated) are removed from the elements in the jQuery set.

and

A handler can also be removed by specifying the function name in the handler argument.

Note that in the latter case you can't specify the function name if the function never had a name in the first place, as in the code in the question where the handler is an anonymous function.

Comments

0

You can use unbind for this .

$('#btnClk).unbind('click');

almost same question

2 Comments

.unbind is the out-of-date way of doing this
I am old school guy .. stackoverflow.com/a/16476792/779158 please check this for new solution . I don't want to copy his answer also in my answer. Thanks for comment.
0

You can do this using off():

$('#btnClk').off('click');

This will remove all click event handlers from the btnClk.

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.