Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change select2 box height

I love the select2 box from https://github.com/ivaynberg/select2 I am using the format: option to format each element, and it looks great.

Everything is fine except the selected element is bigger than the height of the select box due to an image.

I know how to change the width, but how do I change the height so that AFTER the element is selected, it shows the full thing (about 150px)

Here is my initiation:

<script>     $("#selboxChild").select2({         matcher: function(term, text) {              var t = (js_child_sponsors[text] ? js_child_sponsors[text] : text);              return t.toUpperCase().indexOf(term.toUpperCase()) >= 0;          },         formatResult: formatChild,         formatSelection: formatChild,         escapeMarkup: function(m) {             return m;         }     }); </script> 

and here is my select box

<select id="selboxChild" style="width: 300px; height: 200px">     <option value="">No child attached</option> </select> 

To Clarify: I do NOT want the Height of each option to change I am looking for the select box to change height after you select a child.

So when the page first loads it says "No child selected" When you click the drop down and select a child you see the child's image. NOW i need the select box to expand! Otherwise the picture of the child is cut off.

Does anyone understand?

like image 443
relipse Avatar asked Feb 12 '13 02:02

relipse


People also ask

How can we increase height of Select2?

Simply adjust the height till your image fits as desired. You can also use css to set the height of the select2-results (the drop down portion of the select control). 200px is the default height, so change it for the desired height of the drop down.

How do I set Select2 values?

To set selected value of jQuery Select2 with JavaScript, we call val and then trigger . Then we set its value with val to the option with value 0. Finally, we call trigger with 'change' to update the drop down to show the value we set.

How do I add options in Select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).

How do I create a Select2 dynamically?

Select2 jQuery plugin is easy to add on the <select > element. Need to call the select2() method on the selector to initialize. If you adding select2 on a class or select element and when you add an element dynamically then select2 is not initialized on that element.


1 Answers

You should add the following style definitions to your CSS:

.select2-selection__rendered {     line-height: 31px !important; } .select2-container .select2-selection--single {     height: 35px !important; } .select2-selection__arrow {     height: 34px !important; } 
like image 132
Rodrigo Avatar answered Oct 01 '22 08:10

Rodrigo