I am aiming to conditionally display various divs based on certain criteria:
- Show the "x" div if it's not already visible
- Display the "y" div only if the "x" div is already being displayed
- Show the "z" div only if both the "x" and "y" divs are already visible
The following code allows me to toggle a single div between displaying and not displaying:
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
However, when I attempt to do this for multiple divs, it doesn't work as expected.
Here is an example of what I tried:
function myFunction() {
var x = document.getElementById("myDIV");
var y = document.getElementById("myDIV2");
if (x.style.display === "none") {
x.style.display = "block";
} else if (y.style.display === "none") {
y.style.display = "block";
} else {
console.log("It worked!");
}
}
I have created a couple of CodePens to demonstrate these snippets:
I would be grateful for any assistance. Thank you!