From 249226c451d80d73d9d95598e2ddd9be946b0b1d Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Wed, 16 Oct 2013 20:56:10 -0700 Subject: [PATCH] Restore original options / tabindex on destroy(). Fixes #142, #153. --- src/selectize.js | 15 ++++++++++++++- test/api.js | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/selectize.js b/src/selectize.js index 1c4f03c8a..41627e160 100644 --- a/src/selectize.js +++ b/src/selectize.js @@ -193,6 +193,13 @@ $.extend(Selectize.prototype, { self.ignoreHover = false; }); + // store original children and tab index so that they can be + // restored when the destroy() method is called. + this.revertSettings = { + $children : self.$input.children().detach(), + tabindex : self.$input.attr('tabindex') + }; + self.$input.attr('tabindex', -1).hide().after(self.$wrapper); if ($.isArray(settings.items)) { @@ -1735,12 +1742,18 @@ $.extend(Selectize.prototype, { destroy: function() { var self = this; var eventNS = self.eventNS; + var revertSettings = self.revertSettings; self.trigger('destroy'); self.off(); self.$wrapper.remove(); self.$dropdown.remove(); - self.$input.show(); + + self.$input + .html('') + .append(revertSettings.$children) + .attr({tabindex: revertSettings.tabindex}) + .show(); $(window).off(eventNS); $(document).off(eventNS); diff --git a/test/api.js b/test/api.js index 745aaf9df..3166f2170 100644 --- a/test/api.js +++ b/test/api.js @@ -535,6 +535,20 @@ test.selectize.destroy(); expect(has_namespaced_event($('body'), test.selectize.eventNS)).to.be.equal(false); }); + it('should restore original options and tabindex', function() { + var children = '' + + '' + + '' + + '' + + '' + + '' + + '' + + ''; + var test = setup_test('', {}); + test.selectize.destroy(); + expect(test.$select.html()).to.be.equal(children); + expect(test.$select.attr('tabindex')).to.be.equal('9999'); + }); }); });