jQuery-Selectric
jQuery-Selectric copied to clipboard
Add dynamically default values by option
Hello, I modified the plugin locally to fit my needs. Instead of writing every option directly inside the HTML like this :
<select>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
I added an option like that :
$('select').selectric({
...
defaultOptions: ['Option 1', 'Option 2', 'Option 3']
});
To get this result:

If you guys think it could be a good idea to add this feature to Selectric, there is the code to add after the line 408:
if (typeof _this.options.defaultOptions === "object" && _this.options.defaultOptions) {
var defaultOptions = [..._this.options.defaultOptions].map(function (value) {
var newOption = document.createElement('option');
newOption.innerText = value;
return newOption;
});
$options = $([...$options, ...defaultOptions]);
}
It uses ES6 syntaxes so you make sure you'll support it.