JSFiddle - React, Tailwind, and code Playground

by suhail

HTML

<div class="sidenav">
  <a href="#">Home</a>
  <button class="btn">Module 1</button>
  <div class="list">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <button class="btn">Module 2</button>
  <div class="list">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

CSS

* {
  font-family: "Trebuchet MS", sans-serif;
  font-size: 7em;
}

.sidenav {
  margin-top: 0px;
  height: auto;
  width: 200px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #555;
  overflow-x: hidden;
}

.sidenav a,
.btn {
  padding: 10px 10px 10px 20px;
  text-decoration: none;
  font-size: 17px;
  color: #fff;
  display: block;
  text-align: left;
  border: none;
  background: none;
  width: 100%;
  cursor: pointer;
  outline: none;
  border-bottom: 1px solid #777;
}

.sidenav a:hover,
.btn:hover {
  background-color: #777;
}

.list {
  display: none;
  background-color: #999;
  padding-left: 0px;
}

JavaScript

var dropdown = document.getElementsByClassName("btn");
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    var lists = document.getElementsByClassName('list');

    for (var i = 0; i < lists.length; i++) {
      let list = lists[i];
      list.removeAttribute("style");
    }
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}