Shopify themes, liquid, logos, and UX
Hi,
I would like the product tabs to close when another one is opened. How can I best do this?
password: waiheke
Solved! Go to the solution
This is an accepted solution.
Hi @INFRA,
Please go to Actions > Edit code > sections > main-product.liquid file and add code here:
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>
This is an accepted solution.
Hi @INFRA,
Please go to Actions > Edit code > sections > main-product.liquid file and add code here:
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>
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>
Thank you, this is much better for updates 🙂
Hello @INFRA
Please follow the steps below after logging into the Shopify admin:
<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
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025