SpongeBOB
Well-Known Coder
Hi everyone,
When I expand one of the <details> I would like all the others to close automatically.
I've tried :
(in head of the html document within <script>)
But that doesn't work..
Any ideas ?
Thanks.
When I expand one of the <details> I would like all the others to close automatically.
I've tried :
(in head of the html document within <script>)
JavaScript:
// Fetch all the details element.
const details = document.querySelectorAll("details");
// Add the onclick listeners.
details.forEach((targetDetail) => {
targetDetail.addEventListener("click", () => {
// Close all the details that are not targetDetail.
details.forEach((detail) => {
if (detail !== targetDetail) {
detail.setAttribute("open",false);
}
});
});
});
But that doesn't work..
Any ideas ?
Thanks.