...
This solution is used when creating dynamic FAQ sections in Elementor using ACF fields. When FAQ fields are left empty, Elementor still outputs blank accordion items. This script automatically removes empty items and hides the entire FAQ section if no valid FAQs exist. It improves user experience, keeps the layout clean, and eliminates the need for manual cleanup on every post.

What This Does

  • Removes FAQ items with empty question or answer
  • Prevents empty accordion rows from showing
  • Hides the entire FAQ section if no FAQs are filled
  • Automatically opens the first FAQ item
  • Works with Elementor Accordion and Nested Accordion
Step 1: Add CSS Class

Add this class to the OUTER FAQ container (not the accordion widget):

dynamic-post-faq
Step 2: Add JavaScript (Fluent Snippets)

Create a new JavaScript snippet and run it in:

  • Location: Site Wide Footer
  • Status: Active
Step 3: Add Conditional Logic
  • Enable Conditional Logic
  • Post Type → Posts
JavaScript Code


window.addEventListener("load", function () {
  setTimeout(function () {

    const faqSection = document.querySelector(".dynamic-post-faq");
    if (!faqSection) return;

    const items = faqSection.querySelectorAll(
      ".elementor-accordion-item, .e-n-accordion-item, details"
    );

    if (!items.length) return;

    let visibleCount = 0;

    items.forEach(function (item) {
      const text = item.textContent.replace(/\s+/g, " ").trim();

      if (text.length < 20) {
        item.remove();
      } else {
        visibleCount++;
      }
    });

    if (visibleCount === 0) {
      faqSection.remove();
      return;
    }

    const firstItem = faqSection.querySelector(
      ".elementor-accordion-item, .e-n-accordion-item, details"
    );

    if (firstItem) {
      const title =
        firstItem.querySelector(".elementor-tab-title") ||
        firstItem.querySelector(".e-n-accordion-item-title") ||
        firstItem.querySelector("summary");

      if (title) {
        title.click();
      }
    }

  }, 100);
});
Additional Instructions/Information
Make sure the class “dynamic-post-faq” is added to the outer container that wraps the entire FAQ section. Do NOT add the class to the Accordion widget itself. If the code does not work: – Clear website cache – Clear browser cache – Regenerate Elementor CSS (Elementor → Tools) Test cases: – No FAQs → section disappears – Some FAQs → only filled ones show – All FAQs → all show normally This script is reusable across all Elementor + ACF websites.
Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.