If you want to hide and show dynamic elements (in a repeater) then this is the best logic to use (so that the elements do no “blink” when the page loads):
First, set your element to “hidden on load” by using the code panel …
This will hide that element on all of the items of your repeater.
Next you add some code to check if the value of that field in each item has a value or not. If there is no value, then nothing happens because your element is already hidden. If there is a value, then the element only for that item in the repeater will show.
$w.onReady(function () {
$w("#myRepeater").onItemReady(($item, itemData) => {
if (itemData.url) {
$item('#button1').show();
}
});
});
You also mentioned that you will be adding more elements in the future. You simply add on to the code for other “buttons” (or whatever other elements you use) to apply the same logic. For example, let’s say you had 3 buttons in total. This is how the new code would look:
$w.onReady(function () {
$w("#myRepeater").onItemReady(($item, itemData) => {
if (itemData.url) {
$item('#button1').show();
}
if (itemData.anotherUrl) {
$item('#button2').show();
}
if (itemData.anotherField) {
$item('#button3').show();
}
});
});
Now your original sample code you provide implies that you have a dynamic page, with a dynamic dataset and you are trying to hide and show basic elements on the dynamic page that are NOT in a repeater. In that case, the code changes.
The logic would still be the same. You would still set the element to be hidden (or collapsed) on load. But the new sample code would look like this:
$w.onReady(function () {
$w("#Achievements").onReady(() => {
let current = $w("#Achievements").getCurrentItem();
if (current.url) {
$w('#button1').show();
}
if (current.anotherUrl) {
$w('#button2').show();
}
if (current.anotherField) {
$w('#button3').show();
}
});
});
Remember that hide and show are opposites of each other. So if you hide an element, then you must use “show” in the code to show it.
If you use “collapse” then you would use “expand” in the code instead.
Hope that clears things up a little better.
Love,
Code Queen