3

I'm binding a dropdown

("#dropdownlist").change(function(){
    //Do stuff
});

The above code gets called many times.

How can I unbind this event before binding it each time?

1
  • I'm not sure what you're trying to achieve from the question. Do you wan't to bind the event so it occurs only once? Or simply unbind the event after it gets called at a particular point in your script? Commented Nov 5, 2012 at 10:36

4 Answers 4

8

You can use the unbind() method:

$('#dropdownlist').unbind('change');
7

You could use one instead of bind:

("#dropdownlist").one('change',function(){});
2

Use following logic:

var f = function () {

}

$('#xx').bind('change', f); // for bind function f
$('#xx').unbind('change', f); // for unbind unbind function f
2

Additionally jQuery supports namespaces.

For example say you have change handlers that do validation and change handlers that do help text hovering.

You could register:

$("#foo").bind("change.validation", doValidation() );

and

$("#foo").bind("change.helptext", toggleHelptext() );

and then you can unbind a specific set before re-adding validation, e.g.

$("#foo").unbind("change.validation");
$("#foo").bind("change.validation", doValidation() );

HTH Alex

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.