From b746ddbd90491102e14db3964457af8d3ec28c48 Mon Sep 17 00:00:00 2001 From: alexweissman Date: Thu, 12 Oct 2017 11:12:59 -0400 Subject: [PATCH] explain the need to unbind events after calling `destroy` --- .../03.methods/docs.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pages/12.programmatic-control/03.methods/docs.md b/pages/12.programmatic-control/03.methods/docs.md index da4ad105..cb89a936 100644 --- a/pages/12.programmatic-control/03.methods/docs.md +++ b/pages/12.programmatic-control/03.methods/docs.md @@ -49,6 +49,26 @@ The `destroy` method will remove the Select2 widget from the target element. It $('#mySelect2').select2('destroy'); ``` +### Event unbinding + +When you destroy a Select2 control, Select2 will only unbind the events that were automatically bound by the plugin. Any events that you bind in your own code, **including any [Select2 events](/programmatic-control/events) that you explicitly bind,** will need to be unbound manually using the `.off` jQuery method: + +``` +// Set up a Select2 control +$('#example').select2(); + +// Bind an event +$('#example').on('select2:select', function (e) { + console.log('select event'); +}); + +// Destroy Select2 +$('#example').select2('destroy'); + +// Unbind the event +$('#example').off('select2:select'); +``` + ## Examples