Skip to content

Instantly share code, notes, and snippets.

@mudassir0909
Last active September 30, 2017 03:18
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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': {} }
});
@mudassir0909
Copy link
Author

@esbenp
Copy link

esbenp commented Jun 10, 2014

Thank you!! I can confirm, after briefly testing, that it works well with both optgroups and multiple-select.

@federico-bellucci
Copy link

The empty option is no more selectable with the current version of selectize, as you can see in your own fiddle

@tomoguisuru
Copy link

I've updated the fiddle to work again, http://jsfiddle.net/Zw8vm/8/

@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