Collapse and expand image if image has been uploaded in a form

I have an image in a repeater for a reviews section. I have the image linked to the appropriate field in the dataset. I have the image set neither as ‘collapsed’ or ‘hidden’.

I have the code:

if(itemData.image) 
{
    $w('#uploaded').expand();
}
else {
    $w('#uploaded').collapse();
}

and the image will not appear in any repeater box. Upon hitting a ‘load more’ button, when I get to a review within image uploaded it will show the image. But, every image will appear as well with a default selected image.

Not sure what I am doing wrong?

@alloutapparel It’s necessary to tell Wix/Velo which particular repeated item (row) that you want to expand or collapse. There is an explanation of this in the repeater documentation introduction under the section entitled “Retrieve Repeater Item Data When Clicked”. The concept is a little abstract, but it will sink in eventually.

It sounds like you want this logic to be applied when the repeater loads. In that case, you could put it in an onItemReady function. The repeated item scope selector ($item as opposed to the normal selector $w) is the key to having the expand/collapse appropriately applied to each repeater item.

$w("#repeater1").onItemReady( ($item, itemData, index) => {
    if (itemData.image){
        $item('#uploaded').expand();
    } else {
        $item('#uploaded').collapse();
    }
});

In the “load more” button, it would go like this:

$w.onReady( function () {
  $w("#buttonName").onClick( (event) => {
    let $item = $w.at(event.context);
    let itemdata = $item("#myDataset").getCurrentItem();
    if (itemData.image){
        $item('#uploaded').expand();
    } else {
        $item('#uploaded').collapse();
    }
  });
});