64

I am using Select2 3.3.2

I have very very long options in the select. Example:

<select id="e1">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
<option value="WY">very long long long text</option>
<option value="WY">very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text</option>
</select>

The purpose is to avoid wrapping of the options when the dropdown opens up.

I would like to set long width for the dropdown when it opens up (for example - 800px, or even automatic evaluated width).

But to keep the input with short width when the dropdown is closed (for example 300px).

I followed this "Add option to set dropdown width" issue of select2.

But I couldn't get it to work, the width of the options/dropdown is the same as the input's width and the options as wrapped to multiple lines.

Here is the demo of my problem in jsfiddle - http://jsfiddle.net/ewwAX/

Thank you all in advance for helping.

10 Answers 10

120

Select2 includes 'dropdownAutoWidth' parameter which uses javascript to attempt to be wide enough for the dropdown contents.

$('#whatever').select2({dropdownAutoWidth : true});

This at least works with Select2 3.4.3 - I don't know how much earlier it was introduced.

Sign up to request clarification or add additional context in comments.

4 Comments

Works in 4.0.2.
Works in 4.0.6-rc.1
does not work i 4.0.10
It works, but when I select a wider text, the input field also gets widen. So Add this too {dropdownAutoWidth : true, width: 'resolve'}
71

The property you used is not for controlling the dropdown width. You can use the dropdownCssClass property.

Here's a demo in jsFiddle.

JavaScript:

$(document).ready(function() { 
    $("#e1").select2({dropdownCssClass : 'bigdrop'}); 
});

CSS:

.bigdrop {
    width: 600px !important;
}

Please note that for more recent versions there is now a better solution! See Dave Amphlett's answer

4 Comments

Hi, Can You please help me too , I want that the Drop down should be wide as its there in your fiddle, but once selected it should be back to its normal width no matter how wide the date is
@N.K -- See Dave Amphlett's answer below. This works for me.
@N.K if you change the jsfiddle to use a css class (instead of an id) the width property would be used (for the closed state)
no worky here -select2.min.js:1 Uncaught Error: No select2/compat/dropdownCss Is there no way possible to set the width in the latest version? This is driving me bonkers
15
$("#e1").select2({ width: '100%' });      

Try this. This will set the width of container to 100%

2 Comments

This will set the width of container to 100%
This is the only solution that worked for me (out of all the other ones).
9

Even better solution. Use:

$('#e1').select2({width: 'resolve'});

3 Comments

This is now true. This option was not available at the moment of asking the question (2013)
I have tried but it's not working with me ? can you tell me why ?
Thanks. This solves many issues with the width of both the selected box and options.
9

Try this.

$(".js-example-basic-single").select2({dropdownAutoWidth : true});

Comments

4

Try this.

<select id="e1" data-width="100%">

Comments

2

Here's how to make select2 wider only when its open:

If you want it to expand towards the right side, add this css:

  .select2-container.select2-dropdown-open {
    width: 170%;
  }

if you want it to expand left, add this {

  .select2-container.select2-dropdown-open {
    width: 170%;
    margin-left: -70%;
  }

Comments

1

I used this and it worked perfect, only that it will affect all the select2

#select2-drop{
    width: 400px !important;
}

2 Comments

<style> #select2-drop{ width: 400px !important; } </style>
GIving it !important may also not make it responsive
0

This worked for me..

 .select2-container--krajee .select2-dropdown--below{
    width:230px !important;
    margin-left: -70px !important;
}

Comments

0

try customizing your css, add this

.selection {
        width: 80%; 
    }

This worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.