How do I code a checkbox to be checked by default while filtering content from CMS?

Question:
How do I code a checkbox to be checked by default while filtering content from CMS?

Product:
Wix Editor

What are you trying to achieve:
I want a checkbox to be checked by default all the time unless a viewer unchecks it. The checkbox needs to be filtering CMS content. So by unchecking the checkbox, the user is stopping the filtering.

What have you already tried:

import wixData from ‘wix-data’;

$w.onReady(function () {
// Set the checkbox to checked by default
$w(‘#checkbox1’).checked = true;

// Call the filter function to apply the default filter
filterContent();
});

// Function to filter CMS content based on checkbox status
function filterContent() {
// Get the dataset
let dataset = $w(‘#yourDataset’);

// Check the status of the checkbox
let isChecked = $w(‘#checkbox1’).checked;

// Apply filter based on checkbox status
if (isChecked) {
dataset.setFilter(wixData.filter().eq(‘fieldKey’, ‘value’));
} else {
dataset.setFilter(wixData.filter()); // Clear the filter
}
}

// Event handler for checkbox state change
$w(‘#checkbox1’).onChange(() => {
filterContent();
});

Whoa. This feels like you just handed me cheat codes. AMAZING. Thank you. I will go test it out and let you know the results!

Ok so after pasting it in and customizing it, it does not work. I’m guessing there’s some further customization I need to do, or perhaps I did not type in the values correctly. Would you mind taking a look at this screenshot? Here’s what I have in there:

(The checkbox filters correctly when I check it but still remains un-checked by default.)

Here’s a closer view of the code in case it’s too blurry on that full-screen image:

Did you change the IDs?

If by “IDs” you mean the ID of the check box, no. It’s still “checkbox1”

Other than that, have you seen my screenshots? They show all the custom values I added but I have a feeling I may have typed some of them wrong (for example, should I include spaces?) and I may have put them in the wrong place or missed one.

Do you have the dataset named correctly? Your code looks fine.

Ensure that the dataset and fields are accessible and correctly configured. Sometimes, permissions on the dataset can prevent filtering from working.

I don’t know if I have the dataset named correctly. My dataset is called “Portfolio ALL” and I just copied that exactly into the code you gave me. Should be visible in my screenshot.

The field by which I am trying to filter the dataset is just a tag field, and it doesn’t have any custom permissions (or at least I did not set any. I don’t even know what those are)

Look in your CMS and see what you named the dataset.