diff --git a/src/jquery.selectric.js b/src/jquery.selectric.js index e500fda..e0c3753 100644 --- a/src/jquery.selectric.js +++ b/src/jquery.selectric.js @@ -624,13 +624,37 @@ _this.elements.input.val(''); }, _this.options.keySearchTimeout); - if ( val.length ) { - // Search in select options - $.each(_this.items, function(i, elm) { - if ( !elm.disabled && searchRegExp.test(elm.text) || searchRegExp.test(elm.slug) ) { - _this.highlight(i); - return; - } + if (val.length) { + var suitableIndexes = []; + $.each(_this.items, function (i, elm) { + if (elm.element.is('optgroup')) { + $.each(elm.items, function (k, subElm) { + if (!subElm.disabled && (searchRegExp.test(subElm.text) || searchRegExp.test(subElm.slug))) { + suitableIndexes.push(subElm.index); + } + }); + } else if (!elm.disabled && (searchRegExp.test(elm.text) || searchRegExp.test(elm.slug))) { + suitableIndexes.push(elm.index); + } + }); + var alreadyHighlighted = $.inArray(_this.state.highlightedIdx, suitableIndexes) > -1; + var lastItem = suitableIndexes.slice(-1).pop(); + $.each(suitableIndexes, function (i, elm) { + if (alreadyHighlighted) { + if (elm < _this.state.highlightedIdx) { + if (lastItem && lastItem === _this.state.highlightedIdx) { + _this.highlight(elm); + return false; + } + } else if (elm > _this.state.highlightedIdx) { + _this.highlight(elm); + return false; + } + + } else { + _this.highlight(elm); + return false; + } }); } });