|
| 1 | +--- |
| 2 | +title: Basic Usage |
| 3 | +taxonomy: |
| 4 | + category: docs |
| 5 | +process: |
| 6 | + twig: true |
| 7 | +never_cache_twig: true |
| 8 | +--- |
| 9 | + |
| 10 | +## Single select boxes |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +Select2 can take a regular select box like this... |
| 15 | + |
| 16 | +<select class="js-states form-control"></select> |
| 17 | + |
| 18 | +and turn it into this... |
| 19 | + |
| 20 | +<div class="s2-example"> |
| 21 | + <select class="js-example-basic-single js-states form-control"></select> |
| 22 | +</div> |
| 23 | + |
| 24 | +``` |
| 25 | +<select class="js-example-basic-single"> |
| 26 | + <option value="AL">Alabama</option> |
| 27 | + ... |
| 28 | + <option value="WY">Wyoming</option> |
| 29 | +</select> |
| 30 | +``` |
| 31 | + |
| 32 | +<script type="text/javascript" class="js-code-example-basic-single"> |
| 33 | +$(document).ready(function() { |
| 34 | + $(".js-example-basic-single").select2(); |
| 35 | +}); |
| 36 | +</script> |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +``` |
| 41 | +$('.js-example-basic-single').select2(); |
| 42 | +``` |
| 43 | + |
| 44 | +## Multi-select boxes (pillbox) |
| 45 | + |
| 46 | +Select2 also supports multi-value select boxes. The select below is declared with the `multiple` attribute. |
| 47 | + |
| 48 | +<div class="s2-example"> |
| 49 | + <p> |
| 50 | + <select class="js-example-basic-multiple js-states form-control" multiple="multiple"></select> |
| 51 | + </p> |
| 52 | +</div> |
| 53 | + |
| 54 | +``` |
| 55 | +<script type="text/javascript"> |
| 56 | +$(".js-example-basic-multiple").select2(); |
| 57 | +</script> |
| 58 | +
|
| 59 | +<select class="js-example-basic-multiple" multiple="multiple"> |
| 60 | + <option value="AL">Alabama</option> |
| 61 | + ... |
| 62 | + <option value="WY">Wyoming</option> |
| 63 | +</select> |
| 64 | +``` |
| 65 | + |
| 66 | +<script type="text/javascript"> |
| 67 | + $.fn.select2.amd.require([ |
| 68 | + "select2/core", |
| 69 | + "select2/utils" |
| 70 | + ], function (Select2, Utils, oldMatcher) { |
| 71 | + var $basicSingle = $(".js-example-basic-single"); |
| 72 | + var $basicMultiple = $(".js-example-basic-multiple"); |
| 73 | + |
| 74 | + $.fn.select2.defaults.set("width", "100%"); |
| 75 | + |
| 76 | + $basicSingle.select2(); |
| 77 | + $basicMultiple.select2(); |
| 78 | + |
| 79 | + function formatState (state) { |
| 80 | + if (!state.id) { |
| 81 | + return state.text; |
| 82 | + } |
| 83 | + var $state = $( |
| 84 | + '<span>' + |
| 85 | + '<img src="vendor/images/flags/' + |
| 86 | + state.element.value.toLowerCase() + |
| 87 | + '.png" class="img-flag" /> ' + |
| 88 | + state.text + |
| 89 | + '</span>' |
| 90 | + ); |
| 91 | + return $state; |
| 92 | + }; |
| 93 | + }); |
| 94 | + |
| 95 | +</script> |
0 commit comments