Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
how to set max of opened details in html?

what this code does is set the max of opened details to 1 what I want is to set it to 3 I haven't found an answer yet

            const details = document.querySelectorAll("details");
            details.forEach((targetDetail) => {
                targetDetail.addEventListener("click", () => {
                    details.forEach((detail) => {
                        if (detail !== targetDetail) {
                            detail.removeAttribute("open");
                        }
                    });
                });
            });
       <details>
            <summary>test0</summary>
            0
        </details>
     
        <details>
            <summary>test1</summary>
            1
        </details>
        <details>
            <summary>test2</summary>
            2
        </details>
         <details>
            <summary>test3</summary>
            3
        </details>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

An easy solution is to keep track of the nodeList indexes with an Array. I made one for you called openDetails. Then when u open a detail it gets added to the array. When u close it, it gets removed. When u have 3 items u can check the first item of the list, close the corresponding detail and then remove it from the list.

let openDetails = [];

const details = document.querySelectorAll("details");

details.forEach((targetDetail, index) => {
  targetDetail.addEventListener("click", (event) => {
    const detailIsOpen = openDetails.includes(index);

    if (detailIsOpen) {
      openDetails = openDetails.filter((item) => item !== index);
    } else {

      if (openDetails.length > 2) {
        const indexToClose = openDetails[0];
        openDetails = openDetails.filter((item, i) => i !== 0);
        details[indexToClose].removeAttribute("open");
      }

      openDetails.push(index);
    }

  });
});
<details>
  <summary>test0</summary>
  0
</details>

<details>
  <summary>test1</summary>
  1
</details>
<details>
  <summary>test2</summary>
  2
</details>
<details>
  <summary>test3</summary>
  3
</details>

about 4 years ago · Juan Pablo Isaza Report

0

Instead of this,

  const details = document.querySelectorAll("details");
            details.forEach((targetDetail) => {
                targetDetail.addEventListener("click", () => {
                    details.forEach((detail) => {
                        if (detail !== targetDetail) {
                            detail.removeAttribute("open");
                        }
                    });
                });
            });

Try This One, might work for you😉

 const details = document.querySelectorAll("details");
    details.forEach((targetDetail) => {
        targetDetail.addEventListener("click", () => {
            details.forEach((detail) => {
                if (detail !== targetDetail) {
                    details.forEach(element => {
                        element.removeAttribute("open");
                    });
                }
            });
        });
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!