Reputation: 171
I have one dropdown with an options like "Pediatrics". My page is receiving a query parameters as "pediatrics" (lower case).
When I try to set my the selection of my dropdown to Pediatrics, nothings happens, because the dropdown do not have pediatrics.
Ex:
<div class="option" data-selectable="" data-value="Pediatrics">Pediatrics</div>
I`m trying to select this options using:
let myParamS = "pediatrics";
$("#specialization").data('selectize').setValue(myParamS);
=> Will not work, beucase my dropdown have "P"ediatrics instead of "p"ediatrics
Anyone know what is the best way to set this ?
Upvotes: 0
Views: 326
Reputation: 2294
You can loop through options with case insensitive comparison, and find the right option value to set.
let myParamS = "pediatrics";
const selectizeInput = $("#specialization").data('selectize');
// returns a dictionary of options with value as a key
const options = selectizeInput.options;
// does case insensitive comparision to find the matching option
const getValue = (options, valueToFind) => Object.keys(options)
.find(key => key.toLowerCase() === valueToFind.toLowerCase());
// set value
selectizeInput.setValue(getValue(options, myParamS));
Upvotes: 1