Hide Static Title in Webflow: Tutorial for Dynamic List Empty State

Published on
January 15, 2020

How to Hide a Static Title When There Are No Items Inside a Dynamic List in Webflow

If you're using Webflow to create a dynamic website, you may encounter the need to hide a static title if there are no items inside a dynamic list. For example, if you have a list of events and there are no upcoming events, you wouldn't want to display a "Upcoming Events" title with an empty list underneath it. This tutorial will guide you through the process of achieving this in Webflow without needing to write any code.

The Problem

Imagine you have a website with a dynamic list of upcoming events, and you have a static title above the list that says "View Our Upcoming Events." When there are no upcoming events, you don't want to display the static title at all. This is where the need to dynamically hide the title arises.

The Solution in Webflow

  1. Live Example:
    Let's start by understanding the issue in action. In the live example, a collection list is displayed with active items, along with a static title. However, the problem occurs when there are no items in the list. The goal is to hide the static title when the list is empty.

  2. Designer Mode:
    Switch to the Webflow Designer mode to implement a filter that controls the visibility of the elements. By using the "not show" filter, you can make the items in the dynamic list and the static title not appear on the live page when there are no items in the list.

  3. JavaScript:
    To achieve dynamic hiding of the static title based on the presence of items in the dynamic list, some JavaScript will be used. This will allow for the logic that hides the static title when the list is empty and shows it when the list has items.

Implementing the Solution

Webflow Designer Changes

In Webflow's Designer mode, you will need to apply certain classes to the elements that need to be hidden based on the dynamic list's content.

  1. CMS Wrapper and CMS List:
    To work with the dynamic list, two important classes need to be added: hack-30-cms-wrapper and hack-30-cms-list. These classes serve as identifiers for the dynamic list, and they will be used in the JavaScript logic.

  2. Static Title Class:
    Additionally, the static title that needs to be hidden should be assigned the class hack-30-static-title. This class will be used to identify the static title element that should be hidden based on the dynamic list's content.

JavaScript Logic

To implement the logic for hiding the static title based on the dynamic list's content, a small JavaScript snippet needs to be added to the project.

<script>  document.addEventListener("DOMContentLoaded", function() {    if (document.querySelectorAll('.hack-30-cms-list').length === 0) {      document.querySelectorAll('.hack-30-static-title').forEach(function(el) {        el.style.display = 'none';      });    }  });</script>
  1. Explanation:
  • The JavaScript snippet targets the dynamic list class and checks its length. If the length of the dynamic list is 0 (i.e., no items), it hides the static title.
  • By using the querySelectorAll method, it looks for all elements with the hack-30-cms-list class and checks if their length is 0. If it is 0, indicating an empty list, it proceeds to hide the static title elements by setting their display property to none.

By implementing this JavaScript logic, the static title will be dynamically hidden or shown based on the presence of items in the dynamic list.

Extending the Logic

The provided JavaScript logic can be extended to hide or show other elements based on the content of the dynamic list. For instance, you can hide graphics, entire sections, or any other element on the page using the same approach.

Conclusion

By leveraging Webflow's features and incorporating a simple JavaScript snippet, you can effectively hide a static title when there are no items inside a dynamic list. This approach enhances the user experience and ensures that the website remains visually appealing even when certain content is not available.

By following these steps and understanding the logic behind the implementation, you have the foundation to dynamically manage the visibility of elements based on dynamic content in Webflow without having to delve into complex coding.

For more Webflow hacks and tutorials, make sure to follow our YouTube channel and consider subscribing for regular updates. Additionally, you can clone the project mentioned in this tutorial to explore and learn from the complete example.

Thank you for learning this Webflow hack with us, and we hope you find it useful in your web design projects. Happy designing!