Skip to content

Instantly share code, notes, and snippets.

@mudassir0909
Last active September 30, 2017 03:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudassir0909/a6396fdbe528fbe6fbeb to your computer and use it in GitHub Desktop.
Save mudassir0909/a6396fdbe528fbe6fbeb to your computer and use it in GitHub Desktop.
Selectable Placeholder for selectize dropdown plugin
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': {} }
});
@jcxm360
Copy link

jcxm360 commented Aug 4, 2016

Are you able to make it selectable with the keyboard - pressing enter/return?

@kevinpohlmeier
Copy link

I made it work with keyboard enter/return. I also made it compatible with the 'typing_mode' plugin.

https://jsbin.com/yefaguk/1/edit?html,js,output

@kevinpohlmeier
Copy link

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