22

I am working Select2 Select-box.

Problem

Placeholder is not showing in select2. It is always show the first option selected in the select2. It's automatically select first option i want to show the placeholder instead of it.

enter image description here

My Code:

Script:

<script type="text/javascript">
    $(document).ready(function () {
       var data = $('#test_skill').select2({
            placeholder: "Please select an skill",
            allowClear: true
       });
    });

// I have also tried this: This is also not working
    $('#test_skill').select2({
      placeholder: {
        id: '-1', // the value of the option
        text: 'Please select an skill'
      } 
    });
</script>

HTML:

<select class="skills_select2" required name="test_skill" id="test_skill">          
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>
Community
  • 1
  • 1
always-a-learner
  • 3,671
  • 10
  • 41
  • 81

7 Answers7

57

Just put <option></option> in select on first place:

$(document).ready(function() {
  $selectElement = $('#test_skill').select2({
    placeholder: "Please select an skill",
    allowClear: true
  });
});
.skills_select2 {
  width: 120px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" />
<select class="skills_select2" required name="test_skill" id="test_skill">   
    <option></option>
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
14

For placeholders to work properly you have to add an empty option (i.e. ) as a first element to see a placeholder.

From Select2 documentation :

"Note that because browsers assume the first option element is selected in non-multi-value select boxes an empty first option element must be provided () for the placeholder to work."

for example:

<select class="skills_select2" required name="test_skill" id="test_skill"> 
    <option></option>    
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>

and javascript would be:

$("#test_skill").attr(
   "data-placeholder","Please select an skill"
);
rahulkoriya
  • 141
  • 4
6

How about setting it in html

<select>
  <option value="" disabled  selected>Select your option</option>
  <option value="your value ">your value</option>
</select>
Anders Sørensen
  • 426
  • 1
  • 4
  • 14
3

Override templateSelection solve the problem:

templateSelection: function(item) { 
   return item.name  // "name" property of received data from ajax
               || item.text; // "text" value of default selected option or placeholder text
}

Original answer

halfer
  • 19,824
  • 17
  • 99
  • 186
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
  • 1
    The `|| item.text` fixed it for me, as I was using a the templateSelection option and I was only returning the `item.name` value – taekwondoalex Dec 01 '20 at 11:40
3

There is a simple solution for this. Add the data-placeholder attribute and add a empty value option.

<select class="skills_select2" data-placeholder="Please select an option" required name="test_skill" id="test_skill"> 
    <option></option>    
    <option value="1">TEST1</option>
    <option value="2">TEST2</option>
    <option value="3">TEST3</option>            
</select>
nayeemdev
  • 1,201
  • 1
  • 13
  • 19
2

the best way for me is to set it like below: unshift the very first value to the data/result and set the placeholder in select2

$.ajax({
      url: 'api/v1/users',
      type: 'GET',
      success: function(response) {
        let data = $.map(response, function(responseData) {
          return {
            id: responseData.id,
            text: responseData.name
          }
        });
        const selectedElement = $('#user_id');

        selectedElement.html('');

        data.unshift({id: -1, text: '', selected: 'selected', search:'', hidden:true });

        selectedElement.select2({
          theme: 'bootstrap',
          data: data,
          placeholder: {
            id: "-1",
            text: '--- Please select a user ---',
            selected:'selected'
          }
        });
Deano
  • 11,582
  • 18
  • 69
  • 119
Kiry Meas
  • 1,200
  • 13
  • 26
1

Trigger the change event of select

$('#targetSelect').trigger('change');

or

$('#targetSelect').trigger('change.select2'); // Limiting the scope of the change event

Reference : https://select2.org/programmatic-control/events#limiting-the-scope-of-the-change-event

Basit
  • 137
  • 7