Auto close dynamic tab when other tab is opened - Dawn

Solved

Auto close dynamic tab when other tab is opened - Dawn

INFRA
Shopify Partner
234 2 83

Hi,

 

I would like the product tabs to close when another one is opened. How can I best do this?

 

Website

password: waiheke

 

Screenshot 2025-04-09 at 13.01.42.png

Accepted Solution (1)

namphan
Shopify Partner
2572 335 380

This is an accepted solution.

Hi @INFRA,

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

Screenshot.png

Code:

<script>
  document.querySelector('product-info').querySelectorAll('[id^="Details-"]').forEach((details) => {
    details.querySelector('summary').addEventListener('click', (event) => {
      document.querySelector('product-info').querySelectorAll('[id^="Details-"]').forEach((detailsProduct) => {
        if (details !== detailsProduct) {
          detailsProduct.querySelector('summary').setAttribute('aria-expanded', 'false');
          detailsProduct.open = 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 4 (4)

namphan
Shopify Partner
2572 335 380

This is an accepted solution.

Hi @INFRA,

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

Screenshot.png

Code:

<script>
  document.querySelector('product-info').querySelectorAll('[id^="Details-"]').forEach((details) => {
    details.querySelector('summary').addEventListener('click', (event) => {
      document.querySelector('product-info').querySelectorAll('[id^="Details-"]').forEach((detailsProduct) => {
        if (details !== detailsProduct) {
          detailsProduct.querySelector('summary').setAttribute('aria-expanded', 'false');
          detailsProduct.open = 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

tim
Shopify Partner
4247 483 1560

Do not edit your theme code -- this will make theme updates complex.

Create a "Custom liquid" block in your "Product information" section and paste this code:

<script>
  if ( typeof(closeOthers) !='function') {
    window.closeOthers = (event) => {
      let t = event.target.closest('.product details:not([open]');
      if (!t) return;
      let p = t.closest('.product');
      p.querySelectorAll('details[open]').forEach( e => {
        if (e == t ) return;
        e.removeAttribute('open');
        e.querySelector('summary')?.setAttribute('aria-expanded', 'false');
      });
    }

    document.addEventListener('click', closeOthers);
  }
</script>

 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
INFRA
Shopify Partner
234 2 83

Thank you, this is much better for updates 🙂

WebDeskSolution
Shopify Partner
223 51 60

Hello @INFRA 

 

Please follow the steps below after logging into the Shopify admin:

 

  • Go to your Shopify Admin panel.

 

 

  • Click on Online Store > Themes.
  • Find the theme you want to edit and then click Actions > Edit code.
<script>
      window.addEventListener("load", function () {
          document.querySelectorAll("details").forEach((details) => {
              details.addEventListener("click", function () {
                  if (this.open) {
                      document.querySelectorAll("details").forEach((other) => {
                          if (other !== this) {
                              other.removeAttribute("open");
                          }
                      });
                  }
              });
              details.addEventListener("toggle", function () {
                  if (this.open) {
                      document.querySelectorAll("details").forEach((other) => {
                          if (other !== this) {
                              other.removeAttribute("open");
                          }
                      });
                  }
              });
          });
      });
    </script>

Please hit Like and Mark it as a Solution if you find our reply helpful.

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Shopify Partner | Shopify Development Agency | Call: 877.536.3789


Canada: 150 King St W. STE 200, Toronto, ON M5H 1J9

 | USA: 98 Cutter Mill Rd. STE 466, Great Neck, NY 11021