1
0
mirror of synced 2024-11-24 05:46:09 +03:00
select2/docs/pages/03.configuration/02.defaults/docs.md

32 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2017-09-03 03:34:47 +03:00
---
title: Global defaults
taxonomy:
category: docs
---
2017-09-04 08:38:36 +03:00
In some cases, you need to set the default options for all instances of Select2 in your web application. This is especially useful when you are migrating from past versions of Select2, or you are using non-standard options like [custom AMD builds](/getting-started/builds-and-modules#using-select2-with-amd-or-commonjs-loaders). Select2 exposes the default options through `$.fn.select2.defaults`, which allows you to set them globally.
2017-09-03 03:34:47 +03:00
When setting options globally, any past defaults that have been set will be overridden. Default options are only used when an option is requested that has not been set during initialization.
You can set default options by calling `$.fn.select2.defaults.set("key", "value")`. For example:
```
$.fn.select2.defaults.set("theme", "classic");
```
## Nested options
2017-09-04 08:38:36 +03:00
To set a default values for cache, use the same notation used for [HTML `data-*` attributes](/configuration/data-attributes). Two dashes (`--`) will be replaced by a level of nesting, and a single dash (`-`) will convert the key to a camelCase string:
2017-09-03 03:34:47 +03:00
```
$.fn.select2.defaults.set("ajax--cache", false);
```
## Resetting default options
You can reset the default options to their initial values by calling
```
$.fn.select2.defaults.reset();
```