Hi @Dovydas_Sidabras, try using lowercase class name. I don’t know why Webflow allows us to use uppercase class names since on the exported code, all classes are being lowercased and added dashes should the class in the designer have a white space. So in Webflow designer you’ll have “Unlocked” but in the exported code your element will have a class of “unlocked”.
Hi, thanks for your reply. I was really hopping that was the problem, but unfortunately it is not. I’ve changed class names into lowercase and result is the same.
However with some trial-error I discovered few things that might be helpful. Instead of using == I just used = and that took me one step closer because now it kind of works using getElementById ( seriously it only works with one =) however it only applies to the first item on the list, thats why getElementsByClassName seems like better solution, only if it would work of course. The code right now looks like this:
Hi @Dovydas_Sidabras When using getElementsByClassName have you considered using a loop to iterate over the result? I believe that function returns a collection, rather than a single element.
Refer to the very last example on this page for a code block that does this. Perhaps you could try something similar.
<script>
var x = document.getElementsByClassName("unlocked");
var y = document.getElementsByClassName("locked");
if ("webflow field tag" == "Paid") {
for (let i = 0; i < x.length; i++) {
x[i].style.visibility = 'hidden';
}
} else {for (let i = 0; i < y.length; i++) {
y[i].style.visibility = 'visable';
}
}
if ("webflow field tag" == "Paid") {
for (let i = 0; i < y.length; i++) {
y[i].style.visibility = 'visable';
}
} else {for (let i = 0; i < x.length; i++) {
x[i].style.visibility = 'hidden';
}
}
</script>
I had a similar problem, but in addition to hiding CMS collection, I wanted to hide the whole section containing that CMS collection when the CMS collection was empty.
This script targets the div with the id “section_home-events” and adds the .hidden class to it if there are no child elements with the .w-dyn-item class. This will effectively hide the div if it doesn’t contain any elements of the specified class.