I have a dropdown that displays the names of 25 people (so indexes 0-24). How do I change the dropdown to the a value when I don’t know the index position? For example, John Doe is currently selected. I want to change the dropdown to Jane Doe. Normally I would use something like $w(“#dropdown1”).selectedIndex = 6 (assuming that was the index for Jane Doe). However, I know I want to change to the value ‘Jane Doe’ but don’t know the index position. If the wix dropdown api had a selectedValue property, then I could use $w(“#dropdown1”).selectedValue = ‘Jane Doe’.
So, how do I code this?
I would make a small function like this. I think there are fancier ways to find a property but I find just looping to be the easiest and simplest to read
function findIndex ( dropDown , searchTerm ){
for ( let i = 0 ; i < dropDown . options . length ; i ++){
if ( dropDown . options [i]. value == searchTerm ){
return i
}
}
return - 1
}
and call it like this
let foundIndex = findIndex ( $w ( ‘#dropdown1’ ), ‘Jane Doe’ )
$w ( ‘#dropdown1’ ). selectedIndex = foundIndex
if you want to you could do some error handling with the -1 value where it didn’t find what you were looking for
Thanks!! Very nice solution.