In web design, there are often cases where elements should only appear if they contain content. For example, if a product card has an empty feature field, it’s better to hide the block entirely rather than leave an awkward empty space. Unfortunately, Taptop doesn’t yet have a built-in conditional visibility feature. However, this can be easily fixed with a few small JavaScript scripts.
Imagine you have:
Without conditional visibility, empty blocks will take up space, making the page look messy.
window.addEventListener("load", () => {
document.querySelectorAll('[data-check-text]').forEach(container => {
const textBlock = container.querySelector('.text-block-wrap-div');
if (!textBlock || !textBlock.textContent.trim()) {
container.style.display = 'none';
}
});
});
window.addEventListener("load", ...)
– The script runs after the page fully loads.document.querySelectorAll('[data-check-text]')
– Finds all elements with the data-check-text
attribute..forEach(container => { ... })
– Loops through each matching element.const textBlock = container.querySelector('.text-block-wrap-div')
– Looks for the text block inside (Taptop wraps text in .text-block-wrap-div
by default).if (!textBlock || !textBlock.textContent.trim())
– Checks if the text block is emptydata-check-text
attribute to any block that should hide when empty..text-block-wrap-div
(Taptop’s default structure).Important! In Taptop, text blocks are automatically wrapped in .text-block-wrap-div
, even though this layer isn’t visible in the editor.
window.addEventListener("load", () => {
document.querySelectorAll('[data-check-img]').forEach(container => {
const img = container.querySelector('img');
if (!img || !img.src || !img.src.trim()) {
container.style.display = 'none';
}
});
});
data-check-img
.const img = container.querySelector('img')
– Finds the <img>
tag inside the container.if (!img || !img.src || !img.src.trim())
– Checks if the image has no src
(i.e., is empty).data-check-img
to any block containing an image.<img>
tag (Taptop renders images this way).Important! Taptop always uses <img>
for images, so the script targets this tag.
These scripts help automatically hide empty blocks, keeping pages clean and professional. Until Taptop adds native conditional visibility, this is a great workaround.
data-
attributes (data-check-text
or data-check-img
).Now your pages will look polished, no matter how much data your collections contain! 🚀