When attempting to implement a small pop-up window for user confirmation before navigating away from the page, I encountered an issue. Upon clicking the RUN button with the id "openBtn," the pop-up window briefly appears and then disappears. I'm unsure of what went wrong.
CSS
.pop-up{
display: none;
}
.pop-up.active{
display: flex;
position: absolute;
background-color: #fff;
font-size: 30px;
color: #222831;
flex-direction: column;
align-items: center;
height: 150px;
width: 250px;
top: 200px;
left: 170px;
border: 5px double #393E46;
border-radius: 10px;
box-shadow: 2px 4px 15px rgba(0, 0, 0, 0.3);
}
SCRIPT.JS
const openBtn = document.getElementById("openBtn");
const popUp = document.querySelector('.pop-up');
const stayBtn = document.getElementById("stayBtn");
openBtn.addEventListener('click', () =>{
popUp.classList.add('active')
})
stayBtn.addEventListener('click', () =>{
popUp.classList.remove('active')
})
HTML
<div class="controls">
<div class="left"></div>
<form action="" method="post" class="right">
<input type="submit" name="fight" value="FIGHT">
<input type="submit" name="pkmn" value="PKMN">
<input type="submit" name="item" value="ITEM">
<input type="submit" id="openBtn" value="RUN">
</form>
</div>
<div class="pop-up">
<p>Escape battle?</p>
<form action="" method="post" class="buttons">
<input type="submit" class="button" id="stayBtn" name="stay" value="Stay">
<input type="submit" class="button" name="escape" id="run" value="Run">
</form>
</div>
I simply wanted to provide a confirmation option for users before exiting, switching between CSS display properties of none and flex.