Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the better supported offsetWidth property to get a fields width #1172

Merged
merged 2 commits into from
May 14, 2013

Conversation

pfiller
Copy link
Contributor

@pfiller pfiller commented Apr 26, 2013

Fixes #202

@pfiller
Copy link
Contributor Author

pfiller commented Apr 26, 2013

Firefox does not return the correct value for window.getComputedStyle(@form_field).getPropertyValue('width'). Boo. Luckily, it seems like we can use element.offsetWidth and it's supported by all chosen-supported browsers. Yay!

@kenearley
Copy link

@pfiller Issue #212 doesn't seem like the right one here. Are you sure you didn't mean #202?

@kenearley
Copy link

@pfiller The code looks good. Could you give me an example of how to make the issue happen in master? I'm trying to browser test it really quick.

@pfiller
Copy link
Contributor Author

pfiller commented May 2, 2013

You're right - #202 is the one that would be closed.

In master, just view the example page in Chrome and then Firefox. Note the difference in field widths. Sloppy 😦

@kenearley
Copy link

:shipit:

Conflicts:
	chosen/chosen.jquery.min.js
	chosen/chosen.proto.min.js
pfiller added a commit that referenced this pull request May 14, 2013
Use the better supported offsetWidth property to get a fields width
@pfiller pfiller merged commit 0058167 into master May 14, 2013
@pfiller pfiller deleted the firefox-width-disparity branch May 14, 2013 21:25
pfiller added a commit that referenced this pull request Jun 3, 2013
Contributing Guidelines #1236
Wrap for attribute in quotes #963b051ecb
Ensure choice count is reflective of actual selected item count. #1171
Delete choice refactor #1232
Fix scroll issue in Prototype version #1213
Use the better supported offsetWidth property to get a fields width #1172
@oriordan
Copy link

oriordan commented Jun 5, 2013

offsetWidth returns 0 if the element is hidden.
Proposing the following patch:

--- chosen.jquery.js.orig   2013-06-05 14:29:10.125064465 +0000
+++ chosen.jquery.js            2013-06-05 14:23:24.868646488 +0000
@@ -318,6 +318,9 @@
       if (this.options.width != null) {
         return this.options.width;
       } else {
+        if (this.form_field.offsetWidth == 0) {
+            return "" + $(this.form_field).width() + "px";
+        }
         return "" + this.form_field.offsetWidth + "px";
       }
     };

@pfiller
Copy link
Contributor Author

pfiller commented Jun 5, 2013

If the field is hidden, the best workaround is to tell Chosen what width to use explicitly:

$("select").chosen({ width: 'some-width' });

@alexandre-machado
Copy link

but if the parent element is hidden (in case of jquery.modal() is closed and reopened), the field is not available in offsetWidth. only return width if use $(this.form_field).width()

sorry my english

@ReinoutW
Copy link

Pfiller, in that case you cannot specify the width using css.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chosen apply different css (element.style) in different browsers
5 participants