Selectize.js: Data Attributes in Selectize Options

Created on 28 Mar 2014  Â·  7Comments  Â·  Source: selectize/selectize.js

I am using Selectize following the State City example. Is it possible to store some additional data from the Ajax request along with every option in City select?

Say, if I want to store the population along with each option, in one of the following ways...

<option value="Milwaukee" data-population="598916" selected="selected"></option>

<div data-value="Milwaukee" data-selectable="" data-population="598916" class="option selected">Milwaukee</div>

I'd be able to use the option-specific data later without going back to the server.

Most helpful comment

yes, you can do it with render... store any number of values. .. see below

$(ele).selectize({
    delimiter: ';',
    labelField: "label",
    valueField: "value",
    searchField: "label",
    render:       {
        item: function(data) {
    return "<div data-value='"+data.value+"' data-iso='"+data.population+"' data-type='"+data.type+"' class='item'>"+data.label+" </div>";
    }
});

All 7 comments

yes, you can do it with render... store any number of values. .. see below

$(ele).selectize({
    delimiter: ';',
    labelField: "label",
    valueField: "value",
    searchField: "label",
    render:       {
        item: function(data) {
    return "<div data-value='"+data.value+"' data-iso='"+data.population+"' data-type='"+data.type+"' class='item'>"+data.label+" </div>";
    }
});

Thank you!

Is this still working? I came across this issue and could not get it working. After looking around the repo it seems a couple of other guys have run into the problem https://github.com/brianreavis/selectize.js/issues/239 and could not get it working.

Any ideas?

In my case I can't access any data attributes I add in my select after I selectize.

Thanks.

Hi, cannot get this to work trying to read from a loop of options, I need to input data via data-attributes

@artiaggarwal i'm kinda a novice, so sorry for the ask, but I can't seem to figure out how to implement your solution to this issue. I'm basically trying to do the exact same thing @geordee is trying to do by adding data-name="XXX" to the

I'm not totally clear on how the "render" concept works -- sounds like it's re-rendering the

that is created in place of the