Categories
interview

Dropdown ES6

<html>

  <head>
    <style>
      .dropdown {
        position: relative;
        display: inline-block;
      }

      .dropdown-container {
        display: none;
        position: absolute;
        width: 150px;
        border: 1px solid #000;
      }

      .show {
        display: block;
      }

    </style>
    <script>
      const toggle = (e) => {
        document.getElementById('req')
        .classList.toggle('show');
        e.stopPropagation();
      };

      const bodyHandle = (e) => {
        if (document.getElementById('req')
          .classList.contains('show')) {
          document.getElementById('req')
            .classList.toggle('show');
        }
      };

    </script>
  </head>

  <div onclick="bodyHandle(event)"
       style="width: 100vw;
              height: 100vh;
              background-color: lightgreen;">
    <div class="dropdown" onclick="toggle(event)"
         style="background-color: skyblue;">
      <button id="button">Click</button>
      <div id="req" class="dropdown-container"
           style="background-color: lightyellow;">
        <div>item 1</div>
        <div>item 2</div>
        <div>item 3</div>
      </div>
    </div>
  </div>
  </body>

</html>

Demo