From 2813548fbb2054ebfa3c10f51f8fba9210138de6 Mon Sep 17 00:00:00 2001 From: XEuRoMAN Date: Fri, 20 Nov 2015 15:08:37 +0200 Subject: [PATCH] https://github.com/lou/multi-select/issues/205 Fix addOption bugs --- js/jquery.multi-select.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/js/jquery.multi-select.js b/js/jquery.multi-select.js index 71d3b03..9ef084c 100644 --- a/js/jquery.multi-select.js +++ b/js/jquery.multi-select.js @@ -179,10 +179,22 @@ } $.each(options, function(index, option){ if (option.value !== undefined && option.value !== null && - that.$element.find("option[value='"+option.value+"']").length === 0){ - var $option = $(''), - index = parseInt((typeof option.index === 'undefined' ? that.$element.children().length : option.index)), - $container = option.nested == undefined ? that.$element : $("optgroup[label='"+option.nested+"']") + that.$element.find("option[value='"+option.value+"']").length === 0) { + + var $option = $(''); + var $container = that.$element; + + if ('undefined' !== typeof option.nested) { + if (0 === $("optgroup[label='"+option.nested+"']").length) { + var $opt_group_element = $(''); + + $opt_group_element.insertAt($container.children().length, $container); + } + + $container = $("optgroup[label='"+option.nested+"']"); + } + + var index = parseInt((typeof option.index === 'undefined') ? $container.children().length : option.index); $option.insertAt(index, $container); that.generateLisFromOption($option.get(0), index, option.nested);