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

94
Views
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
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!