--- layout: default title: Examples - Select2 slug: examples ---

The basics

Select2 can take a regular select box like this...

and turn it into this...

Example code




    

Multiple select boxes

Select2 also supports multi-value select boxes. The select below is declared with the multiple attribute.

Example code




    

Placeholders

A placeholder value can be defined and will be displayed until a selection is made.

Select2 uses the placeholder attribute on multiple select boxes, which requires IE 10+. You can support it in older versions with the Placeholders.js polyfill.

Example code




    

Loading array data

Select2 provides a way to load the data from a local array.

You can provide initial selections with array data by providing the option tag for the selected values, similar to how it would be done for a standard select.

Example code




    

Loading remote data

Select2 comes with AJAX support built in, using jQuery's AJAX methods. In this example, we can search for repositories using GitHub's API.

When using Select2 with remote data, the HTML required for the select is the same as any other Select2. If you need to provide default selections, you just need to include an option for each selection that contains the value and text that should be displayed.



      

You can configure how Select2 searches for remote data using the ajax option. More information on the individual options that Select2 handles can be found in the options documentation for ajax.



      

Select2 will pass any options in the ajax object to jQuery's $.ajax function, or the transport function you specify.

Disabled mode

Select2 will response the disabled attribute on <select> elements. You can also initialize Select2 with disabled: true to get the same effect.

Example code




    

Disabled results

Select2 will correctly handled disabled results, both with data coming from a standard select (when the disabled attribute is set) and from remote sources, where the object has disabled: true set.

Example code




    

Programmatic access

Select2 supports methods that allow programmatic control of the component.

Example code




    

Limiting the number of selections

Select2 multi-value select boxes can set restrictions regarding the maximum number of options selected. The select below is declared with the multiple attribute with maxSelectionLength in the select2 options

Example code




    

Tagging support

Select2 can be used to quickly set up fields used for tagging.

Note that when tagging is enabled the user can select from pre-existing options or create a new tag by picking the first choice, which is what the user has typed into the search box so far.

Example code




    

Automatic tokenization

Select2 supports ability to add choices automatically as the user is typing into the search field. Try typing in the search field below and entering a space or a comma.

The separators that should be used when tokenizing can be specified using the tokenSeparators options.

Example code




    

Custom matcher

Unlike other dropdowns on this page, this one matches options only if the term appears in the beginning of the string as opposed to anywhere:

This custom matcher uses a compatibility module that is only bundled in the full version of Select2. You also have the option of using a more complex matcher.

Example code




    

Diacritics support

Select2's default matcher will ignore diacritics, making it easier for users to filter results in international selects. Type "aero" into the select below.

Example code




    

Multiple languages

Select2 supports displaying the messages in different languages, as well as providing your own custom messages that can be displayed.

The language does not have to be defined when Select2 is being initialized, but instead can be defined in the [lang] attribute of any parent elements as [lang="es"].

Example code




    

Theme support

Select2 supports custom themes using the theme option so you can style Select2 to match the rest of your application.

These are using the classic theme, which matches the old look of Select2.

Example code




    

RTL support

Select2 will work on RTL websites if the dir attribute is set on the <select> or any parents of it.

You can also use initialize Select2 with dir: "rtl" set.

Example code