Does anybody know if it possible to transfer the element class to the jquery select2 dropdown items?
<select>
<option class="group-1"></option>
<option class="group-1"></option>
<option class="group-2"></option>
<option class="group-2"></option>
</select>
I am trying to build a dynamic list based on a user's selected value from another dropdown, so if a user selects 'group 1' then I wish to only show the items with class 'group-1'. I was hoping to be able to do something like $('ul.select2 li.group-2').hide()
.
EDIT: I am splitting this question into 2 for clarity in the answers I have developed to solve this problem,
- How to preserve option element classes in the select2 listing? - I am providing an answer below for those interested in this.
- How to dynamically filter a select2 listing based on option class? I have opened a new question thread in which I am answering this question as the solutions quite different.