Is there a way to close the dropdown button using Vanilla JS? I have successfully implemented a method to open the dropdown button, but am struggling to find a way to close it. I would like to maintain the for loop for functionality but need guidance on how to achieve the closing action. Any tips on handling this issue would be greatly appreciated. HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/36947df53d.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="logo">
<p id="logo">Logo</p>
<button id="btn"><i class="fa-solid fa-bars fa-2xl"></i></button>
</div>
<nav class ="">
<ul class ="">
<li><a href="#" class="link">Link 1</a></li>
<li><a href="#" class="link">Link 2</a></li>
<li><a href="#" class="link">Link 3</a></li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
JS
let btnn = document.getElementById("btn");
btnn.addEventListener("click", changeBtn);
function changeBtn() {
let links = document.getElementsByClassName('link');
for (let i = 0; i < links.length; i++) {
document.getElementsByClassName('link')[i].style.display = "block";
}
}
CSS
body {
height: 100vh;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.logo {
display: block;
text-align: left;
background: red;
height: 10vh;
}
.logo #logo {
display: inline;
line-height: 10vh;
font-size: 3em;
margin-left: 0.8em;
}
button#btn {
display: inline;
float: right;
margin-right: 2em;
line-height: 10vh;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: block;
list-style-type: none;
margin: 0;
padding-left: 0;
}
nav ul li {
text-align: center;
}
.link {
display: none;
color: white;
font-size: 2.4em;
background-color: blue;
text-decoration: none;
width: 100vw;
height: 7vh;
line-height: 7vh;
border-bottom: 2px solid black;
text-align: center;
}