setting-default-options.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <section>
  2. <h1 id="setting-default-options">Setting default options</h1>
  3. <p>
  4. In some cases, you need to set the default options for all instances of
  5. Select2 in your web application. This is especially useful when you are
  6. migrating from past versions of Select2, or you are using non-standard
  7. options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
  8. default options through <code>$.fn.select2.defaults</code>, which allows
  9. you to set them globally.
  10. </p>
  11. <p>
  12. When setting options globally, any past defaults that have been set will
  13. be overriden. Default options are only used when an option is requested
  14. that has not been set during initialization.
  15. </p>
  16. <p>
  17. <strong>You can set default options</strong> by calling
  18. <code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
  19. set should take the same format as keys set using
  20. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  21. means that two dashes (<code>--</code>) will be replaced by a level of
  22. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  23. string.
  24. </p>
  25. <pre class="prettyprint">
  26. $.fn.select2.defaults.set("theme", "classic");
  27. </pre>
  28. <p>
  29. <strong>You can reset the default options</strong> by calling
  30. <code>$.fn.select2.defaults.reset()</code>.
  31. </p>
  32. </section>