get dropdown options is empty

Hi there,
I’m new to Corvid, but not new to coding and a little lost as to how to proceed.
I have a dropdown populated by data from a collection and it’s working fine, but…
I want to select an item in the dropdown based on the page the visitor came from.
I’ve got the value I need stored into the session properly, (I can see it when I log it to the console,) but when I try to get the values in the dropdown, it comes up empty.

Here is the code I’m using:

$w.onReady( function () {
let value = session.getItem(“provider”);
console.log(“Provider is " + value)
let dropdownOptions = $w(”#provider").options;
console.log(“Dropdown has " + dropdownOptions.length + " options”)
for ( var i=0; i<dropdownOptions.length; i++) {
console.log(“Dropdown value is” + dropdownOptions[i].value)
if (dropdownOptions[i].value === value) {
dropdownOptions.selectedIndex = i;
console.log("dropdown matched " + dropdownOptions[i].value + " with " + value)
}
}
});

The console is showing the name of the provider, then the length of dropdownOptions as 0 and naturally not going into the loop.

Thanks in advance for your support.

Can anyone help with this?

I’ve had to replace it with a text field for now, it would be better if it could be a dropdown that automagically shows the previously visited item

Is $w(" #provider ") a dropdown? Where does it get it’s options from?

Can you provide more information?

Yes, $w(" #provider ") is a drodown.

To fill out the picture a bit more, I have a page with a repeater populated by a collection alled “items”
https://www.culturegateway.org/cultural-directory

The user can drill down into a details page for a fuller description and contact details
https://www.culturegateway.org/cultural-directory/6413ca14-5f61-4589-8b10-a3936131e36c/Aboriginal-Cultural-Awareness-Program

From there the user can click through to a booking page, (that doubles as the contact page and is also accessible from the main menu,) where I want the dropdown to have the item they were just looking at selected, but the placeholder selected if they navigated there from the main menu.
https://www.culturegateway.org/contact

I suspect that $w().onReady is triggered by .onload so it happens after the page is rendered but before any content retrieved by api is there. Couldn’t find an equivalent of onReady for an individual item though and lost as to how to proceed.

Thanks so much for your help.

I think you fixed it as I was looking at it. You just added $w(“#dataset2”).onReady() - That’s just what I was going to tell you was needed.

Another thing to keep in mind… you should keep all import statements at the top.

Yes, once I got it triggering after the data had loaded, that allowed me to debug the inner part where I was trying to set the selectedIndex on the list of options rather than the dropdown itself, so…

dropdownOptions.selectedIndex = i;  

needed to be
$w(" #provider ") .selectedIndex = i;

Thanks for your help.

1 Like