Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder not working in select2

Tags:

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>
like image 428
always-a-learner Avatar asked Sep 11 '17 04:09

always-a-learner


People also ask

How to placeholder in Select2?

js-example-placeholder-single"). select2({ placeholder: "Select a state", allowClear: true }); For single selects only, in order for the placeholder value to appear, you must have a blank <option> as the first option in your <select> control. This is because the browser tries to select the first option by default.

How to add placeholder in Select2 js?

Select2 uses the native placeholder attribute on input boxes for the multiple select, and that attribute is not supported in older versions of Internet Explorer. You need to include Placeholders. js on your page, or use the full build, in order to add placeholder attribute support to input boxes.

How do I change the placeholder text?

The “placeholder” property is used to get or set the placeholder of an input text. This can be used to change the placeholder text to a new one. The element is first selected using a jQuery selector. The new placeholder text can then be assigned to the element's placeholder property.

How do I stop Select2 from auto selecting the first option?

Your best option is to use the placeholder support in Select2 and include a blank option tag as your default selection.


1 Answers

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>
like image 174
Govind Samrow Avatar answered Sep 17 '22 13:09

Govind Samrow