Re: How to make collabsable row close by deafult when clicking on the next row?

Solved

How to make collabsable row close by deafult when clicking on the next row?

sidenkompaniet
Excursionist
26 0 7

How to make collabsable row close by deafult when clicking on the next row?

Accepted Solution (1)
namphan
Shopify Partner
2211 290 328

This is an accepted solution.

Hi @sidenkompaniet,

Please go to Actions > Edit code > sections > main-product.liquid file, find 'schema' and add code here:

Screenshot.png

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>
Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com

View solution in original post

Replies 7 (7)

sherpabot
Shopify Partner
40 3 4

are you using a specific theme, or building the accordion group from scratch? 

namphan
Shopify Partner
2211 290 328

Hi @sidenkompaniet,

Are you referring to the product page accordion? Please explain more about it, I will help you check it

Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com
sidenkompaniet
Excursionist
26 0 7

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😊

namphan
Shopify Partner
2211 290 328

This is an accepted solution.

Hi @sidenkompaniet,

Please go to Actions > Edit code > sections > main-product.liquid file, find 'schema' and add code here:

Screenshot.png

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>
Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com
sidenkompaniet
Excursionist
26 0 7

Thank you so much! So helpful!

namphan
Shopify Partner
2211 290 328

Hi @sidenkompaniet,

You're welcome and nice to meet you

Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com

Dan-From-Ryviu
Shopify Partner
10768 2128 2249

Hi @sidenkompaniet 

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.