Skip to content

Commit 2fb59bb

Browse files
committed
Adding focus and blur events
1 parent acb48e6 commit 2fb59bb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

docs/events.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ selectize.off('event_name', handler);
3232
<td valign="top">value</td>
3333
<td valign="top">Invoked when the value of the control changes.</td>
3434
</tr>
35+
<tr>
36+
<td valign="top"><code>"focus"</code></td>
37+
<td valign="top"></td>
38+
<td valign="top">Invoked when the control gains focus.</td>
39+
</tr>
40+
<tr>
41+
<td valign="top"><code>"blur"</code></td>
42+
<td valign="top"></td>
43+
<td valign="top">Invoked when the control loses focus.</td>
44+
</tr>
3545
<tr>
3646
<td valign="top"><code>"item_add"</code></td>
3747
<td valign="top">value, $item</td>

src/selectize.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ $.extend(Selectize.prototype, {
280280
'option_clear' : 'onOptionClear',
281281
'dropdown_open' : 'onDropdownOpen',
282282
'dropdown_close' : 'onDropdownClose',
283-
'type' : 'onType'
283+
'type' : 'onType',
284+
'focus' : 'onFocus',
285+
'blur' : 'onBlur'
284286
};
285287

286288
for (key in callbacks) {
@@ -489,7 +491,8 @@ $.extend(Selectize.prototype, {
489491
*/
490492
onFocus: function(e) {
491493
var self = this;
492-
494+
var wasFocused = self.isFocused;
495+
493496
self.isFocused = true;
494497
if (self.isDisabled) {
495498
self.blur();
@@ -500,6 +503,8 @@ $.extend(Selectize.prototype, {
500503
if (self.ignoreFocus) return;
501504
if (self.settings.preload === 'focus') self.onSearchChange('');
502505

506+
if (!wasFocused) self.trigger('focus');
507+
503508
if (!self.$activeItems.length) {
504509
self.showInput();
505510
self.setActiveItem(null);
@@ -517,9 +522,13 @@ $.extend(Selectize.prototype, {
517522
*/
518523
onBlur: function(e) {
519524
var self = this;
525+
var wasFocused = self.isFocused;
526+
520527
self.isFocused = false;
521528
if (self.ignoreFocus) return;
522529

530+
if (wasFocused) self.trigger('blur');
531+
523532
self.close();
524533
self.setTextboxValue('');
525534
self.setActiveItem(null);

0 commit comments

Comments
 (0)