Skip to content

Commit 249226c

Browse files
committed
Restore original options / tabindex on destroy(). Fixes #142, #153.
1 parent 60b3080 commit 249226c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/selectize.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ $.extend(Selectize.prototype, {
193193
self.ignoreHover = false;
194194
});
195195

196+
// store original children and tab index so that they can be
197+
// restored when the destroy() method is called.
198+
this.revertSettings = {
199+
$children : self.$input.children().detach(),
200+
tabindex : self.$input.attr('tabindex')
201+
};
202+
196203
self.$input.attr('tabindex', -1).hide().after(self.$wrapper);
197204

198205
if ($.isArray(settings.items)) {
@@ -1735,12 +1742,18 @@ $.extend(Selectize.prototype, {
17351742
destroy: function() {
17361743
var self = this;
17371744
var eventNS = self.eventNS;
1745+
var revertSettings = self.revertSettings;
17381746

17391747
self.trigger('destroy');
17401748
self.off();
17411749
self.$wrapper.remove();
17421750
self.$dropdown.remove();
1743-
self.$input.show();
1751+
1752+
self.$input
1753+
.html('')
1754+
.append(revertSettings.$children)
1755+
.attr({tabindex: revertSettings.tabindex})
1756+
.show();
17441757

17451758
$(window).off(eventNS);
17461759
$(document).off(eventNS);

test/api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,20 @@
535535
test.selectize.destroy();
536536
expect(has_namespaced_event($('body'), test.selectize.eventNS)).to.be.equal(false);
537537
});
538+
it('should restore original options and tabindex', function() {
539+
var children = '<optgroup label="Swedish Cars">' +
540+
'<option value="volvo">Volvo</option>' +
541+
'<option value="saab">Saab</option>' +
542+
'</optgroup>' +
543+
'<optgroup label="German Cars">' +
544+
'<option value="mercedes">Mercedes</option>' +
545+
'<option value="audi">Audi</option>' +
546+
'</optgroup>';
547+
var test = setup_test('<select tabindex="9999">' + children + '</select>', {});
548+
test.selectize.destroy();
549+
expect(test.$select.html()).to.be.equal(children);
550+
expect(test.$select.attr('tabindex')).to.be.equal('9999');
551+
});
538552
});
539553

540554
});

0 commit comments

Comments
 (0)