Home / Admin / WPBakery Collapse All Rows
Duplicate Snippet

Embed Snippet on Your Site

WPBakery Collapse All Rows

Adds Collapse / Expand All Rows in WP Bakery Builder + Ave WP Theme

<10
Code Preview
js
(function($) {
    'use strict';
    /**
     * WP Bakery – Collapse / Expand All Rows (v3)
     * Fixed: injects buttons into the correct visible navbar list position
     * Fixed: iterates ROWS not individual toggles (avoids multi-click cancellation)
     */
    function getFirstToggle(rowEl) {
        var t = rowEl.querySelector('.vc_controls.vc_controls-row .vc_control.column_toggle.vc_column-toggle');
        if (!t) t = rowEl.querySelector('.vc_control.column_toggle.vc_column-toggle');
        return t;
    }
    function collapseAll() {
        document.querySelectorAll('.wpb_vc_row, .wpb_vc_section').forEach(function(row) {
            if (!row.classList.contains('vc_collapsed-row')) {
                var toggle = getFirstToggle(row);
                if (toggle) toggle.click();
            }
        });
    }
    function expandAll() {
        document.querySelectorAll('.wpb_vc_row, .wpb_vc_section').forEach(function(row) {
            if (row.classList.contains('vc_collapsed-row')) {
                var toggle = getFirstToggle(row);
                if (toggle) toggle.click();
            }
        });
    }
    function addButtons() {
        if (document.getElementById('vc-collapse-all-wrap')) return;
        var navUL = document.querySelector('.vc_navbar-nav');
        if (!navUL) return;
        // Find the first pull-right item so we insert BEFORE it (left side of toolbar)
        var firstPullRight = null;
        Array.from(navUL.children).forEach(function(li) {
            if (!firstPullRight && li.className.indexOf('vc_pull-right') > -1) {
                firstPullRight = li;
            }
        });
        var li = document.createElement('li');
        li.id = 'vc-collapse-all-wrap';
        li.style.cssText = 'display:inline-flex;gap:6px;align-items:center;margin:0 8px;';
        li.innerHTML =
            '<a id="vc-collapse-all-btn" href="#" title="Collapse All Rows" ' +
            'style="display:inline-block;padding:4px 10px;background:#2a9d8f;color:#fff;border-radius:3px;font-size:12px;font-weight:600;text-decoration:none;line-height:1.6;">' +
            '&#8861; Collapse All</a>' +
            '<a id="vc-expand-all-btn" href="#" title="Expand All Rows" ' +
            'style="display:inline-block;padding:4px 10px;background:#e76f51;color:#fff;border-radius:3px;font-size:12px;font-weight:600;text-decoration:none;line-height:1.6;">' +
            '&#8862; Expand All</a>';
        if (firstPullRight) {
            navUL.insertBefore(li, firstPullRight);
        } else {
            navUL.appendChild(li);
        }
        document.getElementById('vc-collapse-all-btn').addEventListener('click', function(e) {
            e.preventDefault();
            collapseAll();
        });
        document.getElementById('vc-expand-all-btn').addEventListener('click', function(e) {
            e.preventDefault();
            expandAll();
        });
    }
    function waitForEditor() {
        var attempts = 0;
        var interval = setInterval(function() {
            attempts++;
            if (document.querySelector('.vc_navbar-nav') && document.querySelector('.vc_control.column_toggle')) {
                clearInterval(interval);
                addButtons();
            }
            if (attempts > 40) clearInterval(interval);
        }, 200);
    }
    $(document).ready(function() {
        if (typeof window.vc_iframe === 'undefined' &&
            ($('body').hasClass('js-vc_editor') ||
             window.location.href.indexOf('wpb-backend-editor') > -1 ||
             typeof window.vc !== 'undefined')) {
            waitForEditor();
        }
    });
})(jQuery);

Comments

Add a Comment