Hey there! I have created multiple pop-up elements. To close a pop-up, simply click on the opened popup. I would like to set it up so that when a new pop-up is opened, any previously open pop-ups are automatically closed.
For instance, if I open Pop Up 1 and then proceed to open Pop Up 2, Pop Up 1 should automatically close upon opening Pop Up 2.
Below is the HTML code I am using:
<body style="text-align:center">
<br><br><br>
<div class="popup" onclick="popUp1()">Click me!
<span class="popuptext" id="myPopup1">Popup 1</span>
</div>
<div class="popup" onclick="popUp2()">Click me!
<span class="popuptext" id="myPopup2">Popup 2</span>
</div>
<div class="popup" onclick="popUp3()">Click me!
<span class="popuptext" id="myPopup3">Popup 3</span>
</div>
<script>
function popUp1() {
var popup = document.getElementById("myPopup1");
popup.classList.toggle("show");
}
function popUp2() {
var popup = document.getElementById("myPopup2");
popup.classList.toggle("show");
}
function popUp3() {
var popup = document.getElementById("myPopup3");
popup.classList.toggle("show");
}
</script>
</body>
And here is my CSS styling for the pop-up elements:
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
-moz-animation: fadeIn 1s;
-ms-animation: fadeIn 1s;
animation: fadeIn 1s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity:1 ; }
}
Thank you for your assistance!