When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click?
HTML
<p>Click the button</p>
<button onclick="myFunc()">Try it</button>
<div id="divi">
Div region
</div>
CSS
#divi {
width: 50%;
padding: 50px 0;
text-align: center;
background-color: #ccc;
margin-top:20px;
display:none;
}
Javascript
function myFunc() {
var dv = document.getElementById('divi');
if (dv.style.display ==='none') {
dv.style.display = 'block';
} else {
dv.style.display = 'none';
}
}
Here is the pen: https://codepen.io/animaxf/pen/xdVVbK