diff --git a/select2.js b/select2.js index 208bece6..4df18c80 100755 --- a/select2.js +++ b/select2.js @@ -1044,9 +1044,12 @@ // abstract getContainerWidth: function () { var style, attrs, matches, i, l; + + // see if there is width specified in opts if (this.opts.width !== undefined) return this.opts.width; + // next check if there is inline style on the element that contains width style = this.opts.element.attr('style'); if (style !== undefined) { attrs = style.split(';'); @@ -1057,6 +1060,13 @@ return matches[1]; } } + + // next check if css('width') can resolve a width that is percent based, this is sometimes possible + // when attached to input type=hidden or elements hidden via css + style = this.opts.element.css('width'); + if (style.indexOf("%") > 0) return style; + + // finally, fallback on the calculated width of the element return (this.opts.element.width() === 0 ? 'auto' : this.opts.element.width() + 'px'); } });