How to change placeholder in select2?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to change placeholder in select2?



How do I change the data placeholder with select2?



So far I've tried this.


$("#city").select2({placeholder:"foo"});



And this...


$("#city").attr("data-placeholder","bar");



But neither works.





It should work.. See the fiddle jsfiddle.net/JZQ3Q Please repplicate your issue in this fiddle.
– PSL
Sep 28 '13 at 3:40






5 Answers
5



I found that if I just set the attr e.g. $("#city").attr('data-placeholder', 'bar'), there is no effect. However, if I set the attr and then call $("#city").select2() with no arguments, then the placeholder updates.


$("#city").attr('data-placeholder', 'bar')


$("#city").select2()



e.g.


$("#city").attr("data-placeholder","bar");
$("#city").select2();





You could also just update the placeholder via select2 after it's already been instantiated, without changing the element around (e.g. $("#city").select2({ placeholder: "bar" });).
– redbmk
Apr 30 '15 at 23:21


select2


$("#city").select2({ placeholder: "bar" });





Anyway, (re)applying select2() on the element will also loose any potential option previously set on this element through javascript
– Pierre de LESPINAY
Jul 1 '16 at 12:59




select2()



For other people looking for an attribute solution to avoid changing JS code. I just had to look this up myself for a project, and it seems select2 just needs the attribute data-placeholder="".


<select id="city" data-placeholder="my placeholder"></select>



See more here: select2 options reference





Mind telling me why the downvote? I just tried to be helpful and I do not see what I did wrong in this case?
– Lars Koudal
May 4 '15 at 14:14



Put it in the html that your locator uses like this:


<select id="city" placeholder="my placeholder"></select>





Change to data-placeholder and it works with select2. <select id="city" data-placeholder="my placeholder"></select>
– Lars Koudal
Apr 30 '15 at 22:45




<select id="city" data-placeholder="my placeholder"></select>



You can also do it like this in the template (html) level. Just be sure to assign a blank (e.g. "") string on your first tag.


<select class="test-select2" data-placeholder="Select Anything">
<option value=""></option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>



DEMO



For select2 version 4 I specified the placeholder text in js init code and then update it using $select.select2{placeholder:"updated text..."}


$select.select2{placeholder:"updated text..."}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS