Making Bricks Nestable Accordion’s First Item Open on Desktop and Closed on Mobile

In the Bricks Facebook group, a user asks:

How can I make the nestable accordion first entry open on desktop but closed on mobile?

Here’s a way to do it using a bit of custom JavaScript.

First select your Accordion (Nestable) element in the builder and toggle “Expand first item” on.

Edit your Page/Template with Bricks.

Click on Settings (gear icon) → PAGE SETTINGS → CUSTOM CODE. Add this under Body (footer) scripts:

<script>

document.addEventListener('DOMContentLoaded', function() {
    // Specifically for mobile, check if the first item is open and close it
    if (window.innerWidth <= 768) { // Adjust as needed for your mobile breakpoint
        const firstContent = document.querySelector('#brxe-nmcaww .brxe-block.listening:first-child .accordion-content-wrapper');
        if (firstContent) {
            firstContent.style.display = 'none';
        }
    }
});

</script>

Replace #brxe-nmcaww with the HTML selector of your Accordion (Nestable) element.

Note: This only works after a page refresh 768px and below. You can’t see it take effect if you are resizing the window down from desktop to mobile.

Get access to all 524 Bricks code tutorials with BricksLabs Pro

5 comments

  • I can't get it to work. I'm trying to make it work in a Bricks template. I copied the code and pasted it into the Page Settings --> custom code --> header scripts. Ofcourse I replaced the #brxe-nmcaww with my own.
    What am I doing wrong?

    • A

      Just tested and works fine here.

      Make sure the HTML ID is indeed present in the DOM on the front end for your nestable accordion.

      Also, the code should be added under Body (footer) scripts. I've updated the post to inform the same.

      • Simon Giaccotto

        Great!! Now it works 🙂 It was the body(footer) part.

  • Hi!

    And how modify the code, if I want to open ALL accordions in desktop and closed on mobile. 🙂

    Thanks you help!

Leave your comment

 

Related Tutorials..

Auto-Scroll to the New Open Accordion Item

Auto-Scroll to the New Open Accordion Item

How we can make sure the scroll position is always synced with the accordion when opened.
Categories:
Tags:
Pro
Accordion Grid Query Loop in Bricks

Accordion Grid Query Loop in Bricks

How to set up a accordion in Bricks such that the "titles" are arranged in a grid with the full width content opening below.
Categories:
Pro
Dynamic Accordion in WordPress using Meta Box and Alpine.js

Dynamic Accordion in WordPress using Meta Box and Alpine.js

This Pro tutorial provides the steps to implement an accordion using Alpine.js in WordPress that pulls the values of sub fields of a Meta Box…
Bricks Accordion (Nestable) Element’s Icons

Bricks Accordion (Nestable) Element’s Icons

Adding icon rotations to Bricks' Accordion (Nestable) element when opened.
Categories:
Tags:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Creating a horizontal accordion of featured images from posts output by Bricks' query loop.
Categories:
Tags:
Pro
WooCommerce Product Carousel in Bricks

WooCommerce Product Carousel in Bricks

How to set up a carousel that shows WooCommerce products with the product title, price and Add to Cart button for each.
Categories: