Skip to content

Commit

Permalink
#79
Browse files Browse the repository at this point in the history
Re-bind event handlers before using replaceWith().
  • Loading branch information
wannadream committed Apr 26, 2021
1 parent 53d8a68 commit 7b248a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ hidden.editable-select | Fired when the dropdown has finished being hidden (will
select.editable-select | Fired when an option of the list has been selected. The selected `$element` is available as property of the event.

```javascript
// Must add event handlers before initializing jQuery Editable Select.
$('#editable-select').on('shown.editable-select', function (e) {
// do something...
});
}).editableSelect();
```

## Keyboard support
Expand Down
18 changes: 16 additions & 2 deletions dist/jquery-editable-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Source on GitHub @ https://github.com/indrimuska/jquery-editable-select
*/

+(function ($) {
+(function ($) {
// jQuery Editable Select
EditableSelect = function (select, options) {
var that = this;
Expand All @@ -21,6 +21,13 @@
if (isNaN(this.options.duration) && ['fast', 'slow'].indexOf(this.options.duration) < 0) this.options.duration = 'fast';

// create text input
var events = $._data(select, "events");
if (events) {
Object.keys(events).forEach(key => {
var event = events[key][0];
this.$input.bind(event.type + "." + event.namespace, event.handler);
});
}
this.$select.replaceWith(this.$input);
this.$list.appendTo(this.options.appendTo || this.$input.parent());

Expand Down Expand Up @@ -149,7 +156,14 @@
case 'focus':
that.es.$input
.on('focus', $.proxy(that.es.show, that.es))
.on('blur', $.proxy(that.es.hide, that.es));
.on("blur", $.proxy(function() {
if ($(".es-list:hover").length === 0) {
that.es.hide();
} else {
this.$input.focus();
}
}, that.es
));
break;
case 'manual':
break;
Expand Down
3 changes: 1 addition & 2 deletions dist/jquery-editable-select.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/jquery-editable-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
if (isNaN(this.options.duration) && ['fast', 'slow'].indexOf(this.options.duration) < 0) this.options.duration = 'fast';

// create text input
var events = $._data(select, "events");
if (events) {
Object.keys(events).forEach(key => {
var event = events[key][0];
this.$input.bind(event.type + "." + event.namespace, event.handler);
});
}
this.$select.replaceWith(this.$input);
this.$list.appendTo(this.options.appendTo || this.$input.parent());

Expand Down

0 comments on commit 7b248a1

Please sign in to comment.