Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda