ivaynberg/select2 — pull request #835 by callmevlad (2013-02-09 23:45:08+00:00)
Title: Add 'select' event to fire when an an item is selected

I have a use case where I need to grab the value from a Select2 instance when a value is highlighted, but then restore the value if nothing was actually selected. I can grab the value in the new `highlight` event, but having trouble cleaning up (reverting to the previous value if nothing was selected).

The `change` event doesn't quite work here because it only fires if the value changes, and it fires after the `close` event. So I've added a really simple `select` event to fire when an item is selected, so I can do something like this:

```javascript
var $selected = false,
$fontSize = null,
$oldFontSize = null;

elem.select()
.on('open', function() { $selected = false; $oldFontSize = $fontSize; })
.on('highlight', function(e) { $fontSize = e.val; })
.on('select', function(e) { $selected = true; })
.on('close', function(e) { if (!$selected) { $fontSize = $oldFontSize; } });
```

The code above will update the $fontSize variable 'live' with mouse/keyboard interaction, and revert the value if nothing was actually selected.