Last active
September 30, 2017 03:18
-
-
Save mudassir0909/a6396fdbe528fbe6fbeb to your computer and use it in GitHub Desktop.
Selectable Placeholder for selectize dropdown plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Selectize.define('selectable_placeholder', function( options ) { | |
var self = this; | |
options = $.extend({ | |
placeholder: self.settings.placeholder, | |
html: function ( data ) { | |
return ( | |
'<div class="selectize-dropdown-content placeholder-container">' + | |
'<div data-selectable class="option">' + data.placeholder + '</div>' + | |
'</div>' | |
); | |
} | |
}, options ); | |
// override the setup method to add an extra "click" handler | |
self.setup = (function() { | |
var original = self.setup; | |
return function() { | |
original.apply(this, arguments); | |
self.$placeholder_container = $( options.html( options ) ); | |
self.$dropdown.prepend( self.$placeholder_container ); | |
self.$dropdown.on( 'click', '.placeholder-container', function () { | |
self.setValue( '' ); | |
self.close(); | |
self.blur(); | |
}); | |
}; | |
})(); | |
}); | |
// Usage | |
$( '#my-select-element' ).selectize({ | |
plugins: { 'selectable_placeholder': {} } | |
}); |
I made it work with keyboard enter/return. I also made it compatible with the 'typing_mode' plugin.
Actually, the plugin is no longer necessary. You can simply specify an "allowEmptyOption" setting.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you able to make it selectable with the keyboard - pressing enter/return?