Apologies if this question has been asked before, but I have two onclick divs and I'm trying to achieve a specific behavior. When I open the first one, I want it to automatically close when I open the second one, and vice versa.
Below is the HTML code snippet:
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function myFunction2() {
var x = document.getElementById("myDIV2");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
#myDIV {
position: relative;
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
top: 150px;
}
.button1 {
position: relative;
top: 120px
}
#myDIV2 {
position: relative;
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
}
.button2 {
position: relative;
}
<button class="button1" onclick="myFunction()">Try it</button>
<div id="myDIV">
This is my DIV element.
</div>
<button class="button2" onclick="myFunction2()">Try it2</button>
<div id="myDIV2">
This is my second DIV element.
</div>