Select2 supports methods that allow programmatic control of the component.
## Selecting a value
To programmatically select a value for a Select2 control, use the jQuery `.val()` method:
```
$('select').val('US'); // Select the option with a value of 'US'
$('select').trigger('change'); // Notify any JS components that the value changed
```
Select2 will listen for the `change` event on the `<select>` element that it is attached to. When you make any external changes that need to be reflected in Select2 (such as changing the value), you should trigger this event.
### Limiting the scope of the `change` event
It's common for other components to be listening to the `change` event, or for custom event handlers to be attached that may have side effects. To limit the scope to **only** notify Select2 of the change, use the `.select2` event namespace:
```
$('select').val('US'); // Change the value or make some change to the internal state
$('select').trigger('change.select2'); // Notify only Select2 of changes
```
## Retrieving the selected values
There are two ways to programmatically access the selection data: using `.select2('data')`, or by using a jQuery selector.
### Using the `data` method
Calling `select2('data')` will return a JavaScript array of objects representing the current selection. Each object will contain all of the properties/values that were in the source data objects passed through `processResults` and `templateResult` callbacks (as in <ahref="#data">Loading data from an array</a> and <ahref="#ajax">Connecting to a remote data source</a>).
```
$('select').select2('data');
```
### Using a jQuery selector
Selected items can also be accessed via the `:selected` jQuery selector:
```
$('select').find(':selected');
```
It is possible to extend the `<option>` elements representing selection with the HTML data-* attributes containing arbitrary data from the source data objects: