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

103
Visualizações
Hiding classes by clicking outside

I made a script that when I click on the container it hides a list. However, what I have been trying to do is that the list should hide when clicking outside the container and not inside. I have been looking for answers but nothing really worked for me as I use classes. Does someone know a solution?

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width" />
    <title>Hiding the list</title>
</head>
<body>
<h1>Hiding the list</h1>
<div class="list">Hide the list by clicking outside
    <li>Text1</li>
    <li>Text2</li>
    <li>Text3</li>
</div>

<div class="list">Hide the list by clicking outside
    <li>Text1</li>
    <li>Text2</li>
    <li>Text3</li>
</div>
</body>
</html>

<style>
    .list {
        width: 50%;
        height: 100%;
        background-color: #f2f2f2;
    }
    li {

    }
</style>
<script>
    function hide_list() {
        var children = this.children;

        for (let i = 0; i < children.length; i++) {
            children[i].style.display = "none";
        }
    }
    document.querySelectorAll(".list").forEach(function (elem) {
        elem.addEventListener("click", hide_list);
    });
</script>

about 4 years ago · Juan Pablo Isaza
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