Skip to content

Commit

Permalink
Fix refresh() if select has a title
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyjhol committed May 27, 2015
1 parent c5473f0 commit c09f46c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,21 @@
'</a>';
};

if (this.options.title && !this.multiple && !this.$element.find('.bs-title-option').length) {
liIndex--; // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
// Use native JS to prepend option (faster)
var element = this.$element[0];
titleOption.className = 'bs-title-option';
titleOption.appendChild(document.createTextNode(this.options.title));
titleOption.value = '';
element.insertBefore(titleOption, element.firstChild);
// Check if selected attribute is already set on an option. If not, select the titleOption option.
if (element.options[element.selectedIndex].getAttribute('selected') === null) titleOption.selected = true;
if (this.options.title && !this.multiple) {
// this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
// since liObj is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
liIndex--;

if (!this.$element.find('.bs-title-option').length) {
// Use native JS to prepend option (faster)
var element = this.$element[0];
titleOption.className = 'bs-title-option';
titleOption.appendChild(document.createTextNode(this.options.title));
titleOption.value = '';
element.insertBefore(titleOption, element.firstChild);
// Check if selected attribute is already set on an option. If not, select the titleOption option.
if (element.options[element.selectedIndex].getAttribute('selected') === null) titleOption.selected = true;
}
}

this.$element.find('option').each(function (index) {
Expand Down

0 comments on commit c09f46c

Please sign in to comment.