Very new to JS so bear with me - I'm trying to make a show/hide section on a page. It's working - but it needs to be clicked twice to work. I've looked at event handling and tried a couple examples I found on here but it's not working.
function myFunction() {
var x = document.getElementById("topsection");
var computedStyle = window.getComputedStyle(x);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
#topsection {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
display: none;
}
<div id="topsection">
This is a DIV element.
</div>
<div id="section3">
<button class="togglebutton" onclick="myFunction()">
<img src="image.png" alt="buttonpng";>
</button>
</div>
Any help would be appreciated!