Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 example is not working

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 ?

like image 489
user1740381 Avatar asked Apr 01 '13 17:04

user1740381


People also ask

Why my Select2 is not working?

select2 is not a function" jQuery error occurs for multiple reasons: Forgetting to include the select2 library. Loading the select2 library before the jQuery library. Loading the jQuery library twice.

Why Select2 is not working in modal?

Select2 does not function properly when I use it inside a Bootstrap modal. This issue occurs because Bootstrap modals tend to steal focus from other elements outside of the modal. Since by default, Select2 attaches the dropdown menu to the <body> element, it is considered "outside of the modal".

How do I know if Select2 is open?

select2:open is fired whenever the dropdown is opened. select2:opening is fired before this and can be prevented. select2:close is fired whenever the dropdown is closed. select2:closing is fired before this and can be prevented.

How does Select2 search work?

When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the matcher configuration option.


1 Answers

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
});
like image 149
Gaurav Avatar answered Oct 19 '22 03:10

Gaurav