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?
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?
Share Improve this question edited Oct 5, 2011 at 19:15 yoozer8 7,4897 gold badges62 silver badges96 bronze badges asked Oct 2, 2009 at 10:32 SanthoshSanthosh 20.4k23 gold badges66 silver badges76 bronze badges 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? – hitautodestruct Commented Nov 5, 2012 at 10:36
4 Answers
Reset to default 8You can use the unbind()
method:
$('#dropdownlist').unbind('change');
You could use one instead of bind:
("#dropdownlist").one('change',function(){});
Use following logic:
var f = function () {
}
$('#xx').bind('change', f); // for bind function f
$('#xx').unbind('change', f); // for unbind unbind function f
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