To achieve the desired functionality, it is essential to define two classes:
.visible{
display: block;
}
.invisible{
display: none;
}
Subsequently, utilize JavaScript to dynamically add or remove these classes as needed:
const toggleElements = () =>{
const box1 = document.getElementById("box1")
const box2 = document.getElementById("box2")
if(box1.classList.contains("visible") && box2.classList.contains("invisible")){
box1.classList.remove("visible");
box2.classList.remove("invisible");
box1.classList.add("invisible");
box2.classList.add("visible");
}
if(box1.classList.contains("invisible") && box2.classList.contains("visible")){
box1.classList.remove("invisible");
box2.classList.remove("visible");
box1.classList.add("visible");
box2.classList.add("invisible");
}
}
An alternative approach would be to use a state variable for managing the visibility instead of checking for specific classes.