Skip to content

Commit

Permalink
Restore original options / tabindex on destroy(). Fixes #142, #153.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Oct 17, 2013
1 parent 60b3080 commit 249226c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<optgroup label="Swedish Cars">' +
'<option value="volvo">Volvo</option>' +
'<option value="saab">Saab</option>' +
'</optgroup>' +
'<optgroup label="German Cars">' +
'<option value="mercedes">Mercedes</option>' +
'<option value="audi">Audi</option>' +
'</optgroup>';
var test = setup_test('<select tabindex="9999">' + children + '</select>', {});
test.selectize.destroy();
expect(test.$select.html()).to.be.equal(children);
expect(test.$select.attr('tabindex')).to.be.equal('9999');
});
});

});
Expand Down

0 comments on commit 249226c

Please sign in to comment.