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
Fix for zero-width problem #472
Comments
Duplicate of #92. |
I had to make this change on the fix: |
I have a solution, and this works well in my project: this.f_width = this.form_field_jq.outerWidth(); // add below code under this line at chosen.jqeury.js
|
Hope this helps someone, as it works well in my project. I just added the following CSS rule: .chosen-container
{
width: 100% !important;
} |
Thanks fabioduque ! Setting css for the chosen-container solved my problem! |
You really don't need to set the width in CSS. You can do it right when you initialize Chosen. $(".chosen-select").chosen({ width: '100%' }); // any acceptable CSS width will work |
Actually, I created a KO bindingHandler to generate my chosen elements, and not all of them have a 100% width, so CSS is the way to go for me. Even though I could set the width as an option in my bindingHandler. |
Thanks @fabioduque It also worked for me! |
@pfiller , thank you! that solved my problem. |
why does this happen sometimes? :( |
I used the css approach, because I didn't want to look all the places I used chosen (and other developers might have the same problem when building new views). So the css fixes the problem globally. Thanks @fabioduque |
I was running into this issue with conditional logic in one of my WordPress plugins ... specifically with the width being set to 0px ... even after init (which I queue chosen fields with // This is the original select field
$field = $( '#' + field );
// This is the select field wrapper that I hide (just shown for reference)
$( '.fieldset-' + field ).show();
// Chosen container ID will be original select element ID + _chosen
var $chosen_container = $( '#' + field + '_chosen' );
if( $chosen_container && $chosen_container.width() === 0 ){
$chosen_container.css( 'width', '100%' );
}
$field.trigger( 'chosen:updated' ); |
If anyone else is still having this issue, check when you are calling $(".chosen-select").chosen(). I had a list of javascript function calls on page load, when I moved the .chosen() call to the top of the list it applied the width properly, no more zero width. The functions that I moved it above were retrieving some data and doing a lot of dom operations. |
I like Chosen, but because I use a lot of ajax and hidden panels, I've been having hellish problems with Chosen rendering the selects with zero width, apparently because the width is being calculated before the select is visible.
I added a line that seems to fix this problem (or provide a workaround), at least in my case.
In the Chosen.prototype.set_up_html function, after "this.f_width = this.form_field_jq.outerWidth();", add:
Obviously, this only works if the width is explicitly set in css. But in my case, it solves the problem. A better fix would be welcome.
The text was updated successfully, but these errors were encountered: