Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Select2 placeholder doesn't work

Tags:

I am following this doc http://ivaynberg.github.io/select2/ to create select box with placeholder. My problem is the placeholder doesn't work. Can you help to fix this code?

Code: Also at http://jsfiddle.net/VwGGU/3/

HTML:

<select style="width:300px" id="source">     <optgroup label="Alaskan/Hawaiian Time Zone">         <option value="AK">Alaska</option>         <option value="HI">Hawaii</option>     </optgroup>     <optgroup label="Pacific Time Zone">         <option value="CA">California</option>         <option value="NV">Nevada</option>         <option value="OR">Oregon</option>         <option value="WA">Washington</option>     </optgroup> </select> 

Javascript:

$("#e2").select2({     placeholder: "Select a State",     allowClear: true }); 

The example above shows "Alaska" instead of "Select a State" as placeholder.

UPDATE 1:

Added select2.js now and empty option. It still doesn't show placeholder

HTML

<select style="width:300px" id="source" placeholder="testt test">     <option></option>     <optgroup label="Alaskan/Hawaiian Time Zone">         <option value="AK">Alaska</option>         <option value="HI">Hawaii</option>     </optgroup>     <optgroup label="Pacific Time Zone">         <option value="CA">California</option>         <option value="NV">Nevada</option>         <option value="OR">Oregon</option>         <option value="WA">Washington</option>     </optgroup> 

JS

$(document).ready(function () {     $("#source").select2({         placeholder: "Select a State",         allowClear: true     }); }); 

http://jsfiddle.net/VwGGU/6/

UPDATE 2:

Strange that jsfiddle gave error when I copied .css and .js link from github. This version works

HTML

<select style="width:300px" id="source" >     <option></option>     <optgroup label="Alaskan/Hawaiian Time Zone">         <option value="AK">Alaska</option>         <option value="HI">Hawaii</option>     </optgroup>     <optgroup label="Pacific Time Zone">         <option value="CA">California</option>         <option value="NV">Nevada</option>         <option value="OR">Oregon</option>         <option value="WA">Washington</option>     </optgroup> 

JS

$(document).ready(function () {     $("#source").select2({         placeholder: "Select a State",         allowClear: true     }); }); 

http://jsfiddle.net/VwGGU/9/

like image 842
HP. Avatar asked Feb 06 '14 23:02

HP.


People also ask

How to show placeholder in select2?

$('select'). select2({ templateSelection: function (data) { if (data.id === '') { // adjust for custom placeholder values return 'Custom styled placeholder text'; } return data. text; } }); When multiple selections are allowed, the placeholder will be displayed using the placeholder attribute on the search box.

What is data placeholder?

Data placeholders are used to describe the mapping between the data retrieved from a data source and the layers composing the layout of a data-driven widget.

How do I change the height of my Select 2?

Adding the following is enough: . select2-container . select2-choice { height: 200px; } To have it apply only after an object is selected, add/modify the corresponding css on a change event or similar. This changes the height of the select2 input field, which is not what was asked for.

How do I delete select 2 selection?

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();


2 Answers

Have you done this:

When placeholder is used for a non-multi-value select box, it requires that you include an empty tag as your first option

like image 177
mylesthe.dev Avatar answered Sep 18 '22 09:09

mylesthe.dev


Your select element have an id of source but you are targeting an id of e2 in your jQuery selector, and you need an empty <option> tag

like image 25
Pierre Avatar answered Sep 22 '22 09:09

Pierre