HP.
HP.

Reputation: 19896

jQuery Select2 placeholder doesn't work

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/

Upvotes: 22

Views: 39707

Answers (7)

anayarojo
anayarojo

Reputation: 1205

You should add an empty option tag to the select.

<select data-placeholder="Chose number" data-allow-clear="true" data-width="50%">
    <option></option>
    <option value="1">Number one</option>
    <option value="2">Number two</option>
    <option value="3">Number three</option>
</select>

Check this link Placeholder only works when there's an empty option element

Upvotes: 1

Malladi Bhaskar
Malladi Bhaskar

Reputation: 21

 <script>
         $( "#id").select2({ placeholder: "Select Franchise" });
 </script>

Upvotes: -1

Malladi Bhaskar
Malladi Bhaskar

Reputation: 21

<select id="select2Box" class="form-control brand-select">
    <option value="">Select Franchise</option>
</select>

Upvotes: 1

Raham
Raham

Reputation: 4951

Try this.In html you write the following code.

<select class="select2" multiple="multiple" placeholder="Select State">
    <option value="AK">Alaska</option>
    <option value="HI">Hawaii</option>
</select>

And in your script write the below code.

<script>
         $( ".select2" ).select2( { } );
 </script>

Upvotes: 0

Amar Jagtap
Amar Jagtap

Reputation: 105

$selectElement.select2({
    minimumResultsForSearch: -1,
    placeholder: 'Select Relatives'
}).on('select2-opening', function() {
    $(this).closest('li').find('input').attr('placeholder','Select Relative');
});

Upvotes: 1

mylesthe.dev
mylesthe.dev

Reputation: 9685

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

Upvotes: 45

Pierre
Pierre

Reputation: 1583

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

Upvotes: 30

Related Questions