views:

205

answers:

4

i'm bind a dropdown

("#dropdownlist").change(function(){});

i'm calling the above code so many time.

how can i unbind this event before binding it each time....?

+5  A: 

You could use one instead of bind:

("#dropdownlist").one('change',function(){});
santiiiii
+5  A: 

You can use the unbind() method:

$('#dropdownlist').unbind('change');
Greg
+2  A: 

Use following logic:

var f = function () {

}

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

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

AlexDuggleby