announcements-4.0.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. ---
  2. layout: default
  3. title: Select2 4.0.0 Released
  4. slug: announcements-4.0
  5. ---
  6. <section id="pre-release" class="jumbotron">
  7. <div class="container">
  8. <h1>Announcing Select2 4.0.0</h1>
  9. </div>
  10. </section>
  11. <div class="container s2-docs-container">
  12. <div class="row">
  13. <div class="col-md-9" role="main">
  14. <section id="release">
  15. <h1 class="page-header">Select2 4.0.0</h1>
  16. <p class="lead">
  17. The 4.0 release of Select2 is the result of three years of working on the
  18. code base and watching where it needs to go. At the core, it is a full
  19. rewrite that addresses many of the extensibility and usability problems
  20. that could not be addressed in previous versions.
  21. </p>
  22. <p>
  23. This release contains many breaking changes, but easy-upgrade paths have
  24. been created as well as helper modules that will allow for backwards
  25. compatibility to be maintained with past versions of Select2. Upgrading
  26. <em>will</em> require you to read the release notes carefully, but the
  27. migration path should be relatively straightforward. You can view a list
  28. of the most common changes that you will need to make
  29. <a href="https://github.com/select2/select2/releases">in the release notes</a>.
  30. </p>
  31. <p>
  32. Below is an in-depth review of what is new in Select2, as well as some of
  33. the major changes that have been made.
  34. </p>
  35. </section>
  36. <section id="new">
  37. <h2>New features</h2>
  38. <p>
  39. The notable features of this new release include:
  40. </p>
  41. <ul>
  42. <li>
  43. A more flexible plugin framework that allows you to override Select2 to
  44. behave exactly how you want it to.
  45. </li>
  46. <li>
  47. Consistency with standard <code>&lt;select&gt;</code> elements for all
  48. data adapters, removing the need for hidden <code>&lt;input&gt;</code>
  49. elements.
  50. </li>
  51. <li>
  52. A new build system that uses AMD to keep everything organized.
  53. </li>
  54. <li>
  55. Less specific selectors allowing for Select2 to be styled to fit the
  56. rest of your application.
  57. </li>
  58. </ul>
  59. </section>
  60. <section id="plugins">
  61. <h2>Plugin system</h2>
  62. <p>
  63. Select2 now provides interfaces that allow for it to be easily extended,
  64. allowing for anyone to create a plugin that changes the way Select2 works.
  65. This is the result of Select2 being broken into four distinct sections,
  66. each of which can be extended and used together to create your unique
  67. Select2.
  68. </p>
  69. <p>
  70. The adapters implement a consistent interface that is documented in the
  71. <a href="options.html#adapters">options section for adapters</a>, allowing
  72. you to customize Select2 to do exactly what you are looking for. Select2
  73. is designed such that you can mix and match plugins, with most of the core
  74. options being built as decorators that wrap the standard adapters.
  75. </p>
  76. </section>
  77. <section id="amd-builds">
  78. <h2>AMD-based build system</h2>
  79. <p>
  80. Select2 now uses an
  81. <a href="https://en.wikipedia.org/wiki/Asynchronous_module_definition">AMD-based build system</a>,
  82. allowing for builds that only require the parts of Select2 that you need.
  83. While a custom build system has not yet been created, Select2 is open
  84. source and will gladly accept a pull request for one.
  85. </p>
  86. <p>
  87. Select2 includes the minimal <a href="https://github.com/jrburke/almond">almond</a>
  88. AMD loader, but a custom <code>select2.amd.js</code> build is available
  89. if you already use an AMD loader. The code base (available in the
  90. <code>src</code> directory) also uses AMD, allowing you to include Select2
  91. in your own build system and generate your own builds alongside your
  92. existing infrastructure.
  93. </p>
  94. <p>
  95. The AMD methods used by Select2 are available as
  96. <code>jQuery.fn.select2.amd.define()/require()</code>, allowing you to use the
  97. included almond loader. These methods are primarily used by the
  98. translations, but they are the recommended way to access custom modules
  99. that Select2 provides.
  100. </p>
  101. </section>
  102. <section id="migrating">
  103. <h1>Migrating from Select2 3.5</h1>
  104. <p>
  105. There are a few breaking changes that migrators should be aware of when
  106. they are coming from older versions of Select2.
  107. </p>
  108. <p>
  109. If you use the full build of Select2 (<code>select2.full.js</code>), you
  110. will be automatically notified of the major breaking changes, and
  111. compatibility modules will be used in some cases to ensure that your code
  112. still behaves how you were expecting.
  113. </p>
  114. <h2 id="hidden-input">No more hidden input tags</h2>
  115. <p>
  116. In past versions of Select2, an <code>&lt;input type="hidden" /&gt;</code>
  117. tag was recommended if you wanted to do anything advanced with Select2,
  118. such as work with remote data sources or allow users to add their own
  119. tags. This had the unfortunate side-effect of servers not receiving the
  120. data from Select2 as an array, like a standard <code>&lt;select&gt;</code>
  121. element does, but instead sending a string containing the comma-separated
  122. strings. The code base ended up being littered with special cases for the
  123. hidden input, and libraries using Select2 had to work around the
  124. differences it caused.
  125. </p>
  126. <p>
  127. In Select2 4.0, the <code>&lt;select&gt;</code> element supports all core
  128. options, and support for the old
  129. <code>&lt;input type="hidden" /&gt;</code> has been deprecated. This means
  130. that if you previously declared an AJAX field with some pre-selected
  131. options that looked like…
  132. </p>
  133. {% highlight html linenos %}
  134. <input type="hidden" name="select-boxes" value="1,2,4,6" />
  135. {% endhighlight %}
  136. <p>
  137. It will need to be recreated as a <code>&lt;select&gt;</code> element with
  138. some <code>&lt;option&gt;</code> tags that have <code>value</code>
  139. attributes that match the old value.
  140. </p>
  141. {% highlight html linenos %}
  142. <select name="select-boxes" multiple="multiple">
  143. <option value="1" selected="selected">Select2</option>
  144. <option value="2" selected="selected">Chosen</option>
  145. <option value="4" selected="selected">selectize.js</option>
  146. <option value="6" selected="selected">typeahead.js</option>
  147. </select>
  148. {% endhighlight %}
  149. <p>
  150. The options that you create should have <code>selected="selected"</code>
  151. set so Select2 and the browser knows that they should be selected. The
  152. <code>value</code> attribute of the option should also be set to the value
  153. that will be returned from the server for the result, so Select2 can
  154. highlight it as selected in the dropdown. The text within the option
  155. should also reflect the value that should be displayed by default for the
  156. option.
  157. </p>
  158. <h2 id="new-matcher">Advanced matching of searches</h2>
  159. <p>
  160. In past versions of Select2, when matching search terms to individual
  161. options, which limited the control that you had when displaying results,
  162. especially in cases where there was nested data. The <code>matcher</code>
  163. function was only given the individual option, even if it was a nested
  164. options, without any context.
  165. </p>
  166. <p>
  167. With the new matcher function, only the root-level options are matched and
  168. matchers are expected to limit the results of any children options that
  169. they contain. This allows developers to customize how options within
  170. groups can be displayed, and modify how the results are returned.
  171. </p>
  172. <p>
  173. A function has been created that allows old-style matcher functions to be
  174. converted to the new style. You can retrieve the function from the
  175. <code>select2/compat/matcher</code> module, which should just wrap the old
  176. matcher function.
  177. </p>
  178. <p>
  179. So if your old code used a matcher that only displayed options if they
  180. started with the term that was entered, it would look something like…
  181. </p>
  182. {% highlight js linenos %}
  183. function matchStart (term, text) {
  184. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. $("select").select2({
  190. matcher: matchStart
  191. })
  192. {% endhighlight %}
  193. <p>
  194. Then in Select2 4.0, you would need to wrap the <code>matchStart</code>
  195. method (or the name of the matcher you created) with a
  196. <code>oldMatcher</code> method that we have created.
  197. </p>
  198. {% highlight js linenos %}
  199. function matchStart (term, text) {
  200. if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
  201. return true;
  202. }
  203. return false;
  204. }
  205. $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
  206. $("select").select2({
  207. matcher: oldMatcher(matchStart)
  208. })
  209. });
  210. {% endhighlight %}
  211. <p>
  212. This will work for any matchers that only took in the search term and the
  213. text of the option as parameters. If your matcher relied on the third
  214. parameter containing the jQuery element representing the original
  215. <code>&lt;option&gt;</code> tag, then you may need to slightly change
  216. your matcher to expect the full JavaScript data object being passed in
  217. instead. You can still retrieve the jQuery element from the data object
  218. using the <code>data.element</code> property.
  219. </p>
  220. <h2 id="flexible-placeholders">More flexible placeholders</h2>
  221. <p>
  222. In the most recent versions of Select2, placeholders could only be
  223. applied to the first (typically the default) option in a
  224. <code>&lt;select&gt;</code> if it was blank. The
  225. <code>placeholderOption</code> option was added to Select2 to allow users
  226. using the <code>select</code> tag to select a different option, typically
  227. an automatically generated option with a different value.
  228. </p>
  229. <p>
  230. The <code>placeholder</code> option can now take an object as well as just
  231. a string. This replaces the need for the old
  232. <code>placeholderOption</code>, as now the <code>id</code> of the object
  233. can be set to the <code>value</code> attribute of the
  234. <code>&lt;option&gt;</code> tag.
  235. </p>
  236. <p>
  237. For a select that looks like the following, where the first option (with a
  238. value of <code>-1</code>) is the placeholder option…
  239. </p>
  240. {% highlight html linenos %}
  241. <select>
  242. <option value="-1" selected="selected">Select an option</option>
  243. <option value="1">Something else</option>
  244. </select>
  245. {% endhighlight %}
  246. <p>
  247. You would have previously had to get the placeholder option through the
  248. <code>placeholderOption</code>, but now you can do it through the
  249. <code>placeholder</code> option by setting an <code>id</code>.
  250. </p>
  251. {% highlight js linenos %}
  252. $("select").select2({
  253. placeholder: {
  254. id: "-1",
  255. placeholder: "Select an option"
  256. }
  257. })
  258. {% endhighlight %}
  259. <p>
  260. And Select2 will automatically display the placeholder when the value of
  261. the select is <code>-1</code>, which it will be by default. This does not
  262. break the old functionality of Select2 where the placeholder option was
  263. blank by default.
  264. </p>
  265. <h2 id="value-ordering">Display reflects the actual order of the values</h2>
  266. <p>
  267. In past versions of Select2, choices were displayed in the order that
  268. they were selected. In cases where Select2 was used on a
  269. <code>&lt;select&gt;</code> element, the order that the server received
  270. the selections did not always match the order that the choices were
  271. displayed, resulting in confusion in situations where the order is
  272. important.
  273. </p>
  274. <p>
  275. Select2 will now order selected choices in the same order that will be
  276. sent to the server.
  277. </p>
  278. <h2 id="changed-options">Changed method and option names</h2>
  279. <p>
  280. When designing the future option set for Select2 4.0, special care was
  281. taken to ensure that the most commonly used options were brought over.
  282. For the most part, the commonly used options of Select2 can still be
  283. referenced under their previous names, but there were some changes which
  284. have been noted.
  285. </p>
  286. <h3 id="removed-initselection">
  287. Removed the requirement of <code>initSelection</code>
  288. </h3>
  289. <p>
  290. In the past, whenever you wanted to use a custom data adapter, such as
  291. AJAX or tagging, you needed to help Select2 out in determining the initial
  292. values that were selected. This was typically done through the
  293. <code>initSelection</code> option, which took the underlying data of the
  294. input and converted it into data objects that Select2 could use.
  295. </p>
  296. <p>
  297. This is now handled by
  298. <a href="options.html#dataAdapter">the data adapter</a> in the
  299. <code>current</code> method, which allows Select2 to convert the currently
  300. selected values into data objects that can be displayed. The default
  301. implementation converts the text and value of <code>option</code> elements
  302. into data objects, and is probably suitable for most cases. An example of
  303. the old <code>initSelection</code> option is included below, which
  304. converts the value of the selected options into a data object with both
  305. the <code>id</code> and <code>text</code> matching the selected value.
  306. </p>
  307. {% highlight js linenos %}
  308. {
  309. initSelection : function (element, callback) {
  310. var data = [];
  311. $(element.val()).each(function () {
  312. data.push({id: this, text: this});
  313. });
  314. callback(data);
  315. }
  316. }
  317. {% endhighlight %}
  318. <p>
  319. When using the new <code>current</code> method of the custom data adapter,
  320. <strong>this method is called any time Select2 needs a list</strong> of
  321. the currently selected options. This is different from the old
  322. <code>initSelection</code> in that it was only called once, so it could
  323. suffer from being relatively slow to process the data (such as from a
  324. remote data source).
  325. </p>
  326. {% highlight js linenos %}
  327. $.fn.select2.amd.require([
  328. 'select2/data/array',
  329. 'select2/utils'
  330. ], function (ArrayData, Utils) {
  331. function CustomData ($element, options) {
  332. CustomData.__super__.constructor.call(this, $element, options);
  333. }
  334. Utils.Extend(CustomData, ArrayData);
  335. CustomData.prototype.current = function (callback) {
  336. var data = [];
  337. var currentVal = this.$element.val();
  338. if (!this.$element.prop('multiple')) {
  339. currentVal = [currentVal];
  340. }
  341. for (var v = 0; v < currentVal.length; v++) {
  342. data.push({
  343. id: currentVal[v],
  344. text: currentVal[v]
  345. });
  346. }
  347. callback(data);
  348. };
  349. $("#select").select2({
  350. dataAdapter: CustomData
  351. });
  352. }
  353. {% endhighlight %}
  354. <p>
  355. The new <code>current</code> method of the data adapter works in a similar
  356. way to the old <code>initSelection</code> method, with three notable
  357. differences. The first, and most important, is that <strong>it is called
  358. whenever the current selections are needed</strong> to ensure that Select2
  359. is always displaying the most accurate and up to date data. No matter
  360. what type of element Select2 is attached to, whether it supports a
  361. single or multiple selections, the data passed to the callback
  362. <strong>must be an array, even if it contains one selection</strong>.
  363. The last is that there is only one parameter, the callback to be
  364. executed with the latest data, and the current element that Select2 is
  365. attached to is available on the class itself as
  366. <code>this.$element</code>.
  367. </p>
  368. <p>
  369. If you only need to load in the initial options once, and otherwise will
  370. be letting Select2 handle the state of the selections, you don't need to
  371. use a custom data adapter. You can just create the
  372. <code>&lt;option&gt;</code> tags on your own, and Select2 will pick up
  373. the changes.
  374. </p>
  375. {% highlight js linenos %}
  376. var $element = $('select').select2(); // the select element you are working with
  377. var $request = $.ajax({
  378. url: '/my/remote/source' // wherever your data is actually coming from
  379. });
  380. $request.then(function (data) {
  381. // This assumes that the data comes back as an array of data objects
  382. // The idea is that you are using the same callback as the old `initSelection`
  383. for (var d = 0; d < data.length; d++) {
  384. var item = data[d];
  385. // Create the DOM option that is pre-selected by default
  386. var option = new Option(item.text, item.id, true, true);
  387. // Append it to the select
  388. $element.append(option);
  389. }
  390. // Update the selected options that are displayed
  391. $element.trigger('change');
  392. });
  393. {% endhighlight %}
  394. <h3 id="query-to-data-adapter">
  395. Custom data adapters instead of <code>query</code>
  396. </h3>
  397. <p>
  398. <a href="http://select2.github.io/select2/#data">In the past</a>, any time
  399. you wanted to hook Select2 up to a different data source you would be
  400. required to implement custom <code>query</code> and
  401. <code>initSelection</code> methods. This allowed Select2 to determine the
  402. initial selection and the list of results to display, and it would handle
  403. everything else internally, which was fine more most people.
  404. </p>
  405. <p>
  406. The custom <code>query</code> and <code>initSelection</code> methods have
  407. been replaced by
  408. <a href="options.html#dataAdapter">custom data adapters</a> that handle
  409. how Select2 stores and retrieves the data that will be displayed to the
  410. user. An example of the old <code>query</code> option is provided below,
  411. which is
  412. <a href="http://select2.github.io/select2/#data">the same as the old example</a>,
  413. and it generates results that contain the search term repeated a certain
  414. number of times.
  415. </p>
  416. {% highlight js linenos %}
  417. {
  418. query: function (query) {
  419. var data = {results: []}, i, j, s;
  420. for (i = 1; i < 5; i++) {
  421. s = "";
  422. for (j = 0; j < i; j++) {s = s + query.term;}
  423. data.results.push({id: query.term + i, text: s});
  424. }
  425. query.callback(data);
  426. }
  427. }
  428. {% endhighlight %}
  429. <p>
  430. This has been replaced by custom data adapters which define a similarly
  431. named <code>query</code> method. The comparable data adapter is provided
  432. below as an example.
  433. </p>
  434. {% highlight js linenos %}
  435. $.fn.select2.amd.require([
  436. 'select2/data/array',
  437. 'select2/utils'
  438. ], function (ArrayData, Utils) {
  439. function CustomData ($element, options) {
  440. CustomData.__super__.constructor.call(this, $element, options);
  441. }
  442. Utils.Extend(CustomData, ArrayData);
  443. CustomData.prototype.query = function (params, callback) {
  444. var data = {
  445. results: []
  446. };
  447. for (var i = 1; i < 5; i++) {
  448. var s = "";
  449. for (var j = 0; j < i; j++) {
  450. s = s + params.term;
  451. }
  452. data.results.push({
  453. id: params.term + i,
  454. text: s
  455. });
  456. }
  457. callback(data);
  458. };
  459. $("#select").select2({
  460. dataAdapter: CustomData
  461. });
  462. }
  463. {% endhighlight %}
  464. <p>
  465. The new <code>query</code> method of the data adapter is very similar to
  466. the old <code>query</code> option that was passed into Select2 when
  467. initializing it. The old <code>query</code> argument is mostly the same as
  468. the new <code>params</code> that are passed in to query on, and the
  469. callback that should be used to return the results is now passed in as the
  470. second parameter.
  471. </p>
  472. <h3 id="changed-templating">Renamed templating options</h3>
  473. <p>
  474. Select2 previously provided multiple options for formatting the results
  475. list and selected options, commonly referred to as "formatters", using the
  476. <code>formatSelection</code> and <code>formatResult</code> options. As the
  477. "formatters" were also used for things such as localization,
  478. <a href="#changed-translations">which has also changed</a>, they have been
  479. renamed to <code>templateSelection</code> and <code>templateResult</code>
  480. and their signatures have changed as well.
  481. </p>
  482. <p>
  483. You should refer to the updated
  484. <a href="options.html#templating">documentation on templates</a> when
  485. migrating from previous versions of Select2.
  486. </p>
  487. <h3 id="changed-id">
  488. The <code>id</code> and <code>text</code> properties are strictly enforced
  489. </h3>
  490. <p>
  491. When working with array and AJAX data in the past, Select2 allowed a
  492. custom <code>id</code> function or attribute to be set in various places,
  493. ranging from the initialization of Select2 to when the remote data was
  494. being returned. This allowed Select2 to better integrate with existing
  495. data sources that did not necessarily use the <code>id</code> attribute to
  496. indicate the unique identifier for an object.
  497. </p>
  498. <p>
  499. Select2 no longer supports a custom <code>id</code> or <code>text</code>
  500. to be used, but provides integration points for converting incorrect data
  501. to the expected format.
  502. </p>
  503. <h4>
  504. When working with array data
  505. </h4>
  506. <p>
  507. Select2 previously supported defining array data as an object that matched
  508. the signature of an AJAX response. A <code>text</code> property could be
  509. specified that would map the given property to the <code>text</code>
  510. property on the individual objects. You can now do this when initializing
  511. Select2 by using the following jQuery code to map the old
  512. <code>text</code> and <code>id</code> properties to the new ones.
  513. </p>
  514. {% highlight js linenos %}
  515. var data = $.map([
  516. {
  517. pk: 1,
  518. word: 'one'
  519. },
  520. {
  521. pk: 2,
  522. word: 'two'
  523. }
  524. ], function (obj) {
  525. obj.id = obj.id || obj.pk;
  526. obj.text = obj.text || obj.word;
  527. return obj;
  528. });
  529. {% endhighlight %}
  530. <p>
  531. This will result in an array of data objects that have the <code>id</code>
  532. properties that match the existing <code>pk</code> properties and
  533. <code>text</code> properties that match the existing <code>word</code>
  534. properties.
  535. </p>
  536. <h4>
  537. When working with remote data
  538. </h4>
  539. <p>
  540. The same code that was given above can be used in the
  541. <code>processResults</code> method of an AJAX call to map properties there
  542. as well.
  543. </p>
  544. <h3 id="changed-translations">Renamed translation options</h3>
  545. <p>
  546. In previous versions of Select2, the default messages provided to users
  547. could be localized to fit the language of the website that it was being
  548. used on. Select2 only comes with the English language by default, but
  549. provides
  550. <a href="options.html#language">community-contributed translations</a> for
  551. many common languages. Many of the formatters have been moved to the
  552. <code>language</code> option and the signatures of the formatters have
  553. been changed to handle future additions.
  554. </p>
  555. <h3 id="changed-data">
  556. Declaring options using <code>data-*</code> attributes
  557. </h3>
  558. <p>
  559. In the past, Select2 has only supported declaring a subset of options
  560. using <code>data-*</code> attributes. Select2 now supports declaring all
  561. options using the attributes, using
  562. <a href="options.html#data-attributes">the format specified in the documentation</a>.
  563. </p>
  564. <p>
  565. You could previously declare the URL that was used for AJAX requests using
  566. the <code>data-ajax-url</code> attribute. While Select2 still allows for
  567. this, the new attribute that should be used is the
  568. <code>data-ajax--url</code> attribute. Support for the old attribute will
  569. be removed in Select2 4.1.
  570. </p>
  571. <p>
  572. Although it was not documented, a list of possible tags could also be
  573. provided using the <code>data-select2-tags</code> attribute and passing in
  574. a JSON-formatted array of objects for tags. As the method for specifying
  575. tags has changed in 4.0, you should now provide the array of objects using
  576. the <code>data-data</code> attribute, which maps to
  577. <a href="options.html#data">the array data</a> option. You should also
  578. enable tags by setting <code>data-tags="true"</code> on the object, to
  579. maintain the ability for users to create their own options as well.
  580. </p>
  581. <p>
  582. If you previously declared the list of tags as…
  583. </p>
  584. {% highlight html linenos %}
  585. <select data-select2-tags='[{"id": "1", "text": "One"}, {"id": "2", "text": "Two"}]'></select>
  586. {% endhighlight %}
  587. <p>
  588. …then you should now declare it as…
  589. </p>
  590. {% highlight html linenos %}
  591. <select data-data='[{"id": "1", "text": "One"}, {"id": "2", "text": "Two"}]' data-tags="true"></select>
  592. {% endhighlight %}
  593. <h2 id="removed-methods">Deprecated and removed methods</h2>
  594. <p>
  595. As Select2 now uses a <code>&lt;select&gt;</code> element for all data
  596. sources, a few methods that were available by calling
  597. <code>.select2()</code> are no longer required.
  598. </p>
  599. <h3>.select2("val")</h3>
  600. <p>
  601. The <code>"val"</code> method has been deprecated and will be removed in
  602. Select2 4.1. The deprecated method no longer includes the
  603. <code>triggerChange</code> parameter.
  604. </p>
  605. <p>
  606. You should directly call <code>.val</code> on the underlying
  607. <code>&lt;select&gt;</code> element instead. If you needed the second
  608. parameter (<code>triggerChange</code>), you should also call
  609. <code>.trigger("change")</code> on the element.
  610. </p>
  611. {% highlight js linenos %}
  612. $("select").val("1").trigger("change"); // instead of $("select").select2("val", "1");
  613. {% endhighlight %}
  614. <h3>.select2("enable")</h3>
  615. <p>
  616. Select2 will respect the <code>disabled</code> property of the underlying
  617. select element. In order to enable or disable Select2, you should call
  618. <code>.prop('disabled', true/false)</code> on the
  619. <code>&lt;select&gt;</code> element. Support for the old methods will be
  620. completely removed in Select2 4.1.
  621. </p>
  622. {% highlight js linenos %}
  623. $("select").prop("disabled", true); // instead of $("select").enable(false);
  624. {% endhighlight %}
  625. </section>
  626. </div>
  627. <div class="col-md-3" role="complementary">
  628. {% include nav/announcements-4.0.html %}
  629. </div>
  630. </div>
  631. </div>