From 77de06ecf3642cd59d101f48a1e5ed811a064f2b Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Fri, 6 Jul 2012 12:13:12 +0300 Subject: [PATCH] improve percent width detection. issue #115 --- select2.js | 10 ++++++++++ 1 file changed, 10 insertions(+) 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'); } });