Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic UI Dropdown Option Data Attribute

I've been trying to attach a data-* attribute in a Semantic UI dropdown option but to no success (the data attributes are not being copied to the resulting dropdown options).

Here's the structure of my select:

HTML

<select id='my-dropdown' name='department'>
    <option value='1' data-wsg='something'>Department 1</option>
    <option value='2' data-wsg='another'>Department 2</option>
    . . .
    <option value='n' data-wsg='custom data'>Department N</option>
</select>

jQuery

$("#my-dropdown").dropdown({
    allowAdditions: false,
    fullTextSearch: true,
    onHide: function() {
        // Some codes.
    },
    onChange: function(value, text, choice) {
        // Access the data-wsg attribute of the selected option.
    }
});

I've been reading around a bit but all I saw regarding data attribute support was storing the settings in there. Not really what I need.

Hopefully someone has done something similar and let me know what the solution is.

like image 213
Patrick Gregorio Avatar asked Oct 18 '22 10:10

Patrick Gregorio


1 Answers

Its not pretty, but you can use the details in the onChange to hunt down the data attribute. The functionality you are after was specifically rejected here - https://github.com/Semantic-Org/Semantic-UI/issues/931

onChange: function(value, text, choice) {
    $(this).children('option[value=' + $(choice).data('value') + ']').data('wsg')
}
like image 118
dmoo Avatar answered Nov 03 '22 22:11

dmoo