2017-09-03 03:34:47 +03:00
---
2017-09-04 06:22:18 +03:00
title: Basic usage
2017-09-03 03:34:47 +03:00
taxonomy:
category: docs
process:
twig: true
never_cache_twig: true
---
## Single select boxes
Select2 was designed to be a replacement for the standard `<select>` box that is displayed by the browser. By default it supports all options and operations that are available in a standard select box, but with added flexibility.
Select2 can take a regular select box like this...
< select class = "js-states form-control" > < / select >
and turn it into this...
< div class = "s2-example" >
< select class = "js-example-basic-single js-states form-control" > < / select >
< / div >
```
2017-09-14 03:47:36 +03:00
< select class = "js-example-basic-single" name = "state" >
2017-09-03 03:34:47 +03:00
< option value = "AL" > Alabama< / option >
...
< option value = "WY" > Wyoming< / option >
< / select >
```
< script type = "text/javascript" class = "js-code-example-basic-single" >
$(document).ready(function() {
2017-09-12 00:54:22 +03:00
$('.js-example-basic-single').select2();
2017-09-03 03:34:47 +03:00
});
< / script >
Select2 will register itself as a jQuery function if you use any of the distribution builds, so you can call `.select2()` on any jQuery selector where you would like to initialize Select2.
```
2017-09-12 00:54:22 +03:00
// In your Javascript (external .js resource or < script > t a g )
$(document).ready(function() {
$('.js-example-basic-single').select2();
});
2017-09-03 03:34:47 +03:00
```
2017-09-12 00:54:22 +03:00
>>>>>> The DOM cannot be safely manipulated until it is "ready". To make sure that your DOM is ready before the browser initializes the Select2 control, wrap your code in a [`$(document).ready()` ](https://learn.jquery.com/using-jquery-core/document-ready/ ) block. Only one `$(document).ready()` block is needed per page.
2017-09-03 03:34:47 +03:00
## Multi-select boxes (pillbox)
Select2 also supports multi-value select boxes. The select below is declared with the `multiple` attribute.
< div class = "s2-example" >
< p >
< select class = "js-example-basic-multiple js-states form-control" multiple = "multiple" > < / select >
< / p >
< / div >
2017-09-12 00:54:22 +03:00
**In your HTML:**
2017-09-03 03:34:47 +03:00
2017-09-12 00:54:22 +03:00
```
2017-09-14 03:47:36 +03:00
< select class = "js-example-basic-multiple" name = "states[]" multiple = "multiple" >
2017-09-03 03:34:47 +03:00
< option value = "AL" > Alabama< / option >
...
< option value = "WY" > Wyoming< / option >
< / select >
```
2017-09-12 00:54:22 +03:00
**In your Javascript (external `.js` resource or `<script>` tag):**
```
$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});
```
2017-09-03 03:34:47 +03:00
< script type = "text/javascript" >
$.fn.select2.amd.require([
"select2/core",
"select2/utils"
], function (Select2, Utils, oldMatcher) {
var $basicSingle = $(".js-example-basic-single");
var $basicMultiple = $(".js-example-basic-multiple");
$.fn.select2.defaults.set("width", "100%");
$basicSingle.select2();
$basicMultiple.select2();
function formatState (state) {
if (!state.id) {
return state.text;
}
var $state = $(
'< span > ' +
'< img src = "vendor/images/flags/' +
state.element.value.toLowerCase() +
'.png" class="img-flag" /> ' +
state.text +
'< / span > '
);
return $state;
};
});
< / script >