Dear All,
I have a repeater with 4 dropdowns connected to a collection.
On page load, I would like to put a green background for each dropdown filled out onto the repeater.
![](https://static.wixstatic.com/media/a27d24_eb135850d9b04a26b020c2f880138792~mv2.png/v1/fill/w_731,h_274,al_c,q_85,enc_auto/a27d24_eb135850d9b04a26b020c2f880138792~mv2.png)
With my following code, it is not working.
$w.onReady(function () {
$w("#Repeater").onItemReady(($item, itemData, index) => {
//SUB-CATEGORIES
if ($item("#DropdownSubCategory1").selectedIndex >= 0) {
($item("#DropdownSubCategory1").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory1").style.borderColor = "#B1D3BB");
}
if ($item("#DropdownSubCategory2").selectedIndex >= 0) {
($item("#DropdownSubCategory2").style.backgroundColor = "#B1D3BB")
($item("#DropdownSubCategory2").style.borderColor = "#B1D3BB")
}
if ($item("#DropdownSubCategory3").selectedIndex >= 0) {
($item("#DropdownSubCategory3").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory3").style.borderColor = "#B1D3BB");
}
if ($item("#DropdownSubCategory4").selectedIndex >= 0) {
($item("#DropdownSubCategory4").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory4").style.borderColor = "#B1D3BB");
}
});
});
but if I add “setTimeout(function () {“ for each dropdown, it works.
$w.onReady(function () {
$w("#Repeater").onItemReady(($item, itemData, index) => {
//SUB-CATEGORIES
setTimeout(function () {
if ($item("#DropdownSubCategory1").selectedIndex >= 0) {
($item("#DropdownSubCategory1").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory1").style.borderColor = "#B1D3BB");
}
}, 1000)
setTimeout(function () {
if ($item("#DropdownSubCategory2").selectedIndex >= 0) {
($item("#DropdownSubCategory2").style.backgroundColor = "#B1D3BB")
($item("#DropdownSubCategory2").style.borderColor = "#B1D3BB")
}
}, 1000)
setTimeout(function () {
if ($item("#DropdownSubCategory3").selectedIndex >= 0) {
($item("#DropdownSubCategory3").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory3").style.borderColor = "#B1D3BB");
}
}, 1000)
setTimeout(function () {
if ($item("#DropdownSubCategory4").selectedIndex >= 0) {
($item("#DropdownSubCategory4").style.backgroundColor = "#B1D3BB");
($item("#DropdownSubCategory4").style.borderColor = "#B1D3BB");
}
}, 1000)
});
});
Is my second code correct or is there another way to retype it?
Thank you for your help.
Best regards,
Domivax
Hi 🖐
Why your code doesn't change the colors with the timeout function?
Because when the repeater start populating the items, the page wasn't ready yet, and the timeout function delayed the color change process until the page is ready.
The best practice is to use anything related to a dataset inside its onReady() function, this will make sure the code runs as soon as the dataset is ready, preventing issues like yours from happening.
So, just place the repeater's onItemReady() function inside the dataset's onReady() function, and your code will work.
$w("#myDataset").onReady( () => { $w("#Repeater").onItemReady( ($item, itemData, index) => { // populate the repeater here without the // setTimeout functions }) });
NOTE: Replace the dataset name in orange with yours.
Hope this helps you understand the importance of the onReady() function.
Ahmad