Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda