I have an HTML button that triggers a JavaScript function when clicked, revealing a hidden div by changing its display property. The current setup is functional and achieves the desired outcome.
However, I now wish to modify the functionality so that subsequent clicks toggle the visibility of the div - switching between displayed and not displayed states. My initial approach involves using if-else statements within the function to assign values of zero and one for this toggle effect.
Below is the code snippet I've written in attempting to achieve this toggle behavior:
<Button id="IDButton" type="button" onclick="myFunction()">
<img id="thisIMG" src="thisIMG.png">
</Button>
<script>
var a;
function myFunction() {
if (a === 0) {
document.getElementById("hiddenMenuDiv").style.display = "block";
a = 1;
} else {
document.getElementById("hiddenMenuDiv").style.display = "none";
a = 0;
}
}
</script>