Preserve custom HTML5 data attributes
Feature request
Preserve custom HTML5 data attributes, so that in this case data-fruit won't disappear when going from <select> to Selectize:
http://jsfiddle.net/k44p2by6/
+1
I've created a plugin to override -not extend- the function which changes the original select options.
Selectize.define('preserve_original_select', function(options) {
var self = this;
this.updateOriginalInput = (function() {
var original = self.updateOriginalInput;
return function() {
var i, n, options, self = this;
if (self.$input[0].tagName.toLowerCase() === 'select') {
for (i = 0, n = self.items.length; i < n; i++) {
self.$input.find('[value="'+self.items[i]+'"]').prop('selected', true);
}
} else {
self.$input.val(self.getValue());
}
if (self.isSetup) {
self.trigger('change', self.$input.val());
}
// original.apply(this, arguments);
};
})();
});
Don't forget to load the plugin.
Plugin is good but data-attributes in OPTIONs shouldn't disappear by default. It is hurt for transparent selectizing of existing SELECT elements.
Yes! Selectize should be updated to allow for the preservation of custom attributes in each option.
+1
Would really appreciate a fix to this. Can't get plugin working.
+1
Agreed, selectize should retain data attributes
+1
I have no idea how I got CC'd on all the posts to this list, but I'd appreciate it if it stopped. Thanks!
On Sep 1, 2014, at 8:05 AM, Stephan Helbig [email protected] wrote:
+1
— Reply to this email directly or view it on GitHub.
+1
+1
+1
+1
This is working.
$(ele).selectize({ ..... dataAttr: 'data-extra', //default is data-data ..... } });
Add data-extra in every option as json encoded, Ex option value="one" data-extra="{'test':'123'}" option value="two" data-extra="{'test':'456'}"
Get the selectize instance using this, var selectize = $(ele)[0].selectize
Users selected options can be found here: selectize.items
Data attributes of all options can be found here: selectize.options
In our case it will be, selectize.options['one'].test selectize.options['two'].test
Let me know if this is working.
I think that it's already possibile to do that without extending the plugin (certainly, a full support will be appreciated) simply using the onInitialize method in combination with the revertSettings attribute (jsfiddle):
$('select').selectize({
onInitialize: function () {
var s = this;
this.revertSettings.$children.each(function () {
$.extend(s.options[this.value], $(this).data());
});
}
});
The possibilities are infinite with this feature, like a FULL HTML SELECT.
@indrimuska You are a hero sir. That works like charm!
@JamesWilsonVSolvit :smile: thank you man!
works like charm, but not for option groups as their are undefined :(. any idea how to get to next level.. ( get each from option group and keep custom-data ? )
Hi @kimek, as you can see, revertSettings has a $children attribute that contains all the previous children of the original select you define.
If your select has got many <optgroup>s, the $children attribute contains these jquery elements, not the <option>s you want. That's why you get undefined as value of those datas.

The simplest way to overcome this problem is to look for the <option>s inside the <optgroup>s as you normally do in jQuery (.find()).
Here it is an example.