Updated on 5 Nov 2024
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.
Update 1: If you are using Frames Accordion add this code instead:
<script>
document.addEventListener('DOMContentLoaded', function() {
// Function to handle mobile accordion state
const handleMobileAccordion = () => {
if (window.innerWidth <= 768) {
const firstAccordionItem = document.querySelector('.fr-faq-accordion-alpha .fr-accordion__item:first-child');
if (firstAccordionItem) {
const header = firstAccordionItem.querySelector('.fr-accordion__header');
const body = firstAccordionItem.querySelector('.fr-accordion__body');
const icon = firstAccordionItem.querySelector('.fr-accordion__icon');
// Remove expanded state
header.setAttribute('aria-expanded', 'false');
// Reset body height to 0
body.style.height = '0px';
// Remove flipped class from icon if present
if (icon) {
icon.classList.remove('fr-accordion__icon--flipped');
}
}
}
};
// Run on initial load
handleMobileAccordion();
// Run on resize
window.addEventListener('resize', handleMobileAccordion);
});
</script>
For multiple Frames accordions on the same page add this instead:
document.addEventListener('DOMContentLoaded', function() {
// Function to handle mobile accordion state
const handleMobileAccordions = () => {
if (window.innerWidth <= 768) {
// Get all accordions on the page
const accordions = document.querySelectorAll('[data-fr-accordion-options]');
accordions.forEach(accordion => {
// Parse the accordion options
const options = JSON.parse(accordion.dataset.frAccordionOptions || '{}');
// Only process accordions that have firstItemOpened set to true
if (options.firstItemOpened) {
const firstAccordionItem = accordion.querySelector('.fr-accordion__item:first-child');
if (firstAccordionItem) {
const header = firstAccordionItem.querySelector('.fr-accordion__header');
const body = firstAccordionItem.querySelector('.fr-accordion__body');
const icon = firstAccordionItem.querySelector('.fr-accordion__icon');
// Remove expanded state
header.setAttribute('aria-expanded', 'false');
// Reset body height to 0
body.style.height = '0px';
// Remove flipped class from icon if present
if (icon) {
icon.classList.remove('fr-accordion__icon--flipped');
}
}
}
});
}
};
// Run on initial load
handleMobileAccordions();
// Run on resize with debounce
let resizeTimer;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(handleMobileAccordions, 250);
});
});
7 comments
Ronny
I simply added style and block in the custom attribute section of the content part. No code at all needed
Manos G
Really useful. Thanks!
Simon
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?
Sridhar Katakam
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.
Patrik
Hi!
And how modify the code, if I want to open ALL accordions in desktop and closed on mobile. :-)
Thanks you help!
Sridhar Katakam
Hey Patrik, you could use the new conditional accordion feature of our BricksExtras' Pro Accordion element for this w/o writing any code.
See near the bottom of https://bricksextras.com/docs/pro-accordion/.