I have searched for a method online on how to change the visibility of my HTML element using JavaScript, but for some reason, all my attempts at making this work have failed, and I can’t recognize why. What’s going wrong here, why is it not working?
<!DOCTYPE html>
<html>
<header>
<script src="app.js"></script>
<link rel="stylesheet" type="text/css" href="./style.css" />
</header>
<button id="toggle">Hi</button>
<div id="block"></div>
</html>
const targetDiv = document.getElementById("block");
const btn = document.getElementById("toggle");
btn.onclick = function() {
if (targetDiv.style.display !== "none") {
targetDiv.style.display = "none";
}
else {
targetDiv.style.display = "block";
}
};
#block {
display: none;
position: fixed;
height: 20%;
width: 50%;
background-color: #00ff55;
}