0

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>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 2
    Your HTML is invalid. List items must be children of a list. – isherwood Mar 31 '22 at 19:40
  • 1
    It's also invalid to have content outside the head or body elements. (I've removed those when tidying up your demo.) – isherwood Mar 31 '22 at 19:41
  • [this question](https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element) shows how to do it with jQuery. Converting it to vanilla JS should be straightforward. – Barmar Mar 31 '22 at 19:42
  • Does this answer your question? [How do I detect a click outside an element?](https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element) – isherwood Mar 31 '22 at 19:42
  • 1
    @isherwood Please don't ever modify the code of a question that isn't yours. Now the code does not give a true representation of what the OP is working with and therefore even with the main problem solved, other problems may still persist. – Scott Marcus Mar 31 '22 at 19:45
  • Barmar & isherwood Well the thing is I actually try to avoid using jQuery not because I dont like it but because I have basically no knowledge of it. I have looked up for answers which support classes and JavaScript Vanilla but havent found one with this description – ProgramFreak Mar 31 '22 at 19:48
  • Please clarify what "outside the list" means to you? Where exactly is someone supposed to click in order to hide, say the second list? – Scott Marcus Mar 31 '22 at 19:48
  • @ScottMarcus, what change do you claim was problematic? All I did is format. It was logically and functionally identical (and much easier to see). – isherwood Mar 31 '22 at 19:50
  • @ScottMarcus the main idea is that the person should click wherever he wants except at the container (class ="list") With the first click it should remove the first list, with the second click it should remove the second list. So basically it should work like my script with the only difference that the function runs when the click is outside of the container. – ProgramFreak Mar 31 '22 at 19:53
  • The question I linked has many answers on it, including some without jQuery involvement. It answers your question, if indirectly. You need to pick what elements to watch. – isherwood Mar 31 '22 at 19:54
  • you can always add a sibling div which can cover the entire background with positioning, then make that click event on the background div. – David Grosh Mar 31 '22 at 19:57
  • @DavidGrosh well this is an idea that I am able to realize! But the question is -> Would it be still clean code to do that? – ProgramFreak Mar 31 '22 at 20:02
  • 1
    as long as it's readable that that extra div is intended as a background to be clicked on, I would not see any issues with that. – David Grosh Mar 31 '22 at 20:06
  • @isherwood I have looked up the answers and while yes there is one which provides the answer I am looking for, I am not a big fan of assigning every element a different attribute value. But still thanks for the answer! – ProgramFreak Mar 31 '22 at 20:07
  • 1
    I don't think that's what's proposed. You just need an event handler on an outer element, even if it's the document. See https://stackoverflow.com/a/3028037/1264804. – isherwood Mar 31 '22 at 20:09
  • alright well I will check that out thank you very much! – ProgramFreak Mar 31 '22 at 20:14

0 Answers0