Code working in onReady but not button click?

Hi,
I am using the following code in the pages onReady function and it is working as expected, however, the same code is not returning the same results in an button onClick(). Is there a reason for this?

$w("#datasetMembersOnly").onReady(() => {
        $w("#rptMembers").forEachItem(($w, itemData, index) => {
        if (itemData.approved === "1") {
                $w("#boxName").style.backgroundColor = "#FFFFFF";
                $w("#boxCompany").style.backgroundColor = "#FFFFFF";
                $w("#boxJobTitle").style.backgroundColor = "#FFFFFF";
                $w("#boxEmailAddress").style.backgroundColor = "#FFFFFF";
                $w("#btnApprove").hide();
                $w("#btnDisable").show(); 
            } else {
                $w("#boxName").style.backgroundColor = "#7FCCF7";
                $w("#boxCompany").style.backgroundColor = "#7FCCF7";
                $w("#boxJobTitle").style.backgroundColor = "#7FCCF7";
                $w("#boxEmailAddress").style.backgroundColor = "#7FCCF7";
                $w("#btnApprove").show();
                $w("#btnDisable").hide(); 
            }
            console.log(itemData.approved)
        });
    });

The button is definitely being clicked because I’ve added an additional console log but it is not reaching the if/else statement as nothing is displayed in the log for itemData.approved?

Thanks,
Rachel

Hi,
The code looks fine, please share your editor’s URL so we can inspect.

Hi, many thanks for taking a look, it’s driving me insane!! :wink:

The site is ‘MHA Members’ and the url is https://www.markhatterassociates.co.uk

The page to look at is ‘Approve Members’

The button click now seems to be reaching the ‘if’ statement but is not displaying the correct formatting and it is still not reaching the ‘else’ statement.

Hi, did you get a chance to look at this? Thanks :wink:

Hi,
I checked your site, it seems like you are trying to display all the items in the database after you filtered them.
In order to do that you need to clear the filter (filter the dataset without parameters) and then go through the items.
Try this code for you button click event and let me know if you figured it out :slight_smile:

export function btnAllMembers_click(event) {
    $w('#datasetMembersOnly').setFilter(wixData.filter())
        .then(() => {
            $w("#rptMembers").forEachItem(($w, itemData, index) => {
                  if (itemData.approved === "1") {
                    $w("#boxName").style.backgroundColor = "#FFFFFF";
                    $w("#boxCompany").style.backgroundColor = "#FFFFFF";
                    $w("#boxJobTitle").style.backgroundColor = "#FFFFFF";
                    $w("#boxEmailAddress").style.backgroundColor = "#FFFFFF";
                    $w("#btnApprove").hide();
                    $w("#btnDisable").show();
                } else {
                    $w("#boxName").style.backgroundColor = "#7FCCF7";
                    $w("#boxCompany").style.backgroundColor = "#7FCCF7";
                    $w("#boxJobTitle").style.backgroundColor = "#7FCCF7";
                    $w("#boxEmailAddress").style.backgroundColor = "#7FCCF7";
                    $w("#btnApprove").show();
                    $w("#btnDisable").hide();
                }
                console.log(itemData.approved)
            });
        });
    console.log("all members button clicked!")
}

Exactly what I was looking for! Thank you so much for taking the time to look over this for me :slight_smile: