Skip to content
Advertisement

Looping through selectpicker options

I am working with a selectpicker that contains optgroup and option elements. I would like to loop through the options and “select” all options that have a value starting with a certain string. For example, if I have the following:

JavaScript

and some buttons “Vegetable” and “Fruit”, I would like all of the options that have values starting with “V.” to be selected when the user clicks the “Vegetable” button. How can I do this using the selectpicker ‘val’ option? Thanks!

Advertisement

Answer

JavaScript
JavaScript

I am not sure if you actually need to select for V or F in this situation but since that is what you asked for that is what I did. The two are not mutually exclusive so if you need that that requires more code.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement