I am working Select2
Select-box.
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.
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>
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.
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.
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.
Your best option is to use the placeholder support in Select2 and include a blank option tag as your default selection.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With