Shopify themes, liquid, logos, and UX
How to make collabsable row close by deafult when clicking on the next row?
Solved! Go to the solution
This is an accepted solution.
Hi @sidenkompaniet,
Please go to Actions > Edit code > sections > main-product.liquid file, find 'schema' and add code here:
Code:
<script>
const productContent = document.querySelector('#MainProduct-{{ section.id }}');
const listAccordion = productContent.querySelectorAll('.product__accordion details');
listAccordion.forEach((accordion) => {
accordion.addEventListener('click', (event) => {
if (event.currentTarget.hasAttribute('open') === false ) {
listAccordion.forEach((otherAccordion) => {
if (otherAccordion !== event.currentTarget) {
const summaryElement = otherAccordion.querySelector('summary');
otherAccordion.removeAttribute('open');
summaryElement.setAttribute('aria-expanded', false);
}
})
}
});
})
</script>
are you using a specific theme, or building the accordion group from scratch?
Hi @sidenkompaniet,
Are you referring to the product page accordion? Please explain more about it, I will help you check it
https://4d61f0-d9.myshopify.com/products/sidenklanning-zigzag-svart
Yes on the product page! Thank you very much! I want it to close when you click on the next one😊
This is an accepted solution.
Hi @sidenkompaniet,
Please go to Actions > Edit code > sections > main-product.liquid file, find 'schema' and add code here:
Code:
<script>
const productContent = document.querySelector('#MainProduct-{{ section.id }}');
const listAccordion = productContent.querySelectorAll('.product__accordion details');
listAccordion.forEach((accordion) => {
accordion.addEventListener('click', (event) => {
if (event.currentTarget.hasAttribute('open') === false ) {
listAccordion.forEach((otherAccordion) => {
if (otherAccordion !== event.currentTarget) {
const summaryElement = otherAccordion.querySelector('summary');
otherAccordion.removeAttribute('open');
summaryElement.setAttribute('aria-expanded', false);
}
})
}
});
})
</script>
Thank you so much! So helpful!
Hi @sidenkompaniet,
You're welcome and nice to meet you
You can do that by adding this code to theme.liquid file, before </body> in Online Store > Themes > Edit code
<script>
document.addEventListener('DOMContentLoaded', function() {
const accordions = document.querySelectorAll('.product__accordion details');
accordions.forEach(accordion => {
accordion.addEventListener('toggle', function() {
if (this.open) {
// Close all other accordions
accordions.forEach(otherAccordion => {
if (otherAccordion !== this) {
otherAccordion.removeAttribute('open');
}
});
}
});
});
});
</script>
- Solved it? Hit Like and Accept solution! ❤️Buy Me Coffee❤️
- Reton: Loyalty & Rewards - Earn points through tasks, redeem for discounts, and enjoy exclusive VIP rewards!
- Ryviu - Reviews & QA app: Collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy Gallery: Lookbook Image: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024