Select2 example is not working
Asked Answered
G

2

8

I am trying to use Select2.js plugin. I have created a fiddle of the simplest example of select2 but i can't figure out why this is not working ?

http://jsbin.com/edeseh/2/edit

you can see in the fiddle that selection of items in selectlist and allow clear is not working.

Can anybody please help me, what i am doing wrong here ?

Galop answered 1/4, 2013 at 17:45 Comment(0)
Z
11

You should use options value to enable selection

and

you should use an empty option for showing placeholder and allowClear option.

Here is the working fiddle :

http://jsbin.com/edeseh/8/edit

Main code copied from the link:

HTML:

<select id="e2" style="width:200px;">
  <option value=""><option>
  <option value="1">Mustard</option>
  <option value="2">Ketchup</option>
  <option value="3">Relish</option>
</select>

JavaScript:

$("#e2").select2({
  placeholder: "Select a State",
  allowClear: true
});
Zeba answered 1/4, 2013 at 17:59 Comment(4)
Nice. The Select2 docs make no mention of an empty option for the placeholder. It's easy to assume that it will be prepended automatically.Putrescent
The docs now mention placing an empty option - look under "Placeholders": select2.github.io/options.htmlPinto
Is it just me or is the fiddle not working? It just shows the old style select?Endue
jsbin.com/huhewujira/edit?html,js,output Updated JsBin Link. In original answer CDN links used in jsbin were expired.Lichter
G
0

Older versions require you to add 'select2' class in the dropdown.

Goingson answered 23/8, 2019 at 5:57 Comment(2)
Can you please explain this a little more? :)Pshaw
add "select2" as class in dropdown which is a requirement in older versionsGoingson

© 2022 - 2024 — McMap. All rights reserved.