shankyyyyyyy
shankyyyyyyy

Reputation: 97

HTML/VanillaJS : Modal not opening

I'm trying to create a modal that pops up when a button is clicked. I am just mentioning the part of the code which I think is relevant

Code:

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("btn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.btn {
  border-radius: 25px;
  position: absolute;
  right: 150px;
  width: 200px;
  height: 120px;
  padding: 14px 28px;
  font-color: white;
  cursor: pointer;
  background-color: #0096FF;
  border: none;
  padding: 15px 32px;
  text-align: center;
  display: inline-block;
  font-size: 36px;
  margin: 4px 2px;
  cursor: pointer;
  font-style: Brush Script MT;
}
<div id="btn" class="btn">Open</div>


<div id="myModal" class="modal">


  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Welcome</p>
  </div>

</div>

But for some reason, the modal is not opening up. There are other divs in the code, and I'm not sure they are responsible. Please help

Upvotes: 0

Views: 41

Answers (1)

Not A Robot
Not A Robot

Reputation: 2684

First, you need to hide your modal.

#myModal{
  display: none;
}

Add the above code to your CSS.

Secondly, you need a z-index on your button when the modal is active.

btn.style.zIndex = -1;

Use the above code inside your JavaScript, when the button is pressed.

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("btn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
  btn.style.zIndex = -1;
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
#myModal{
 display: none;
}
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.btn {
  border-radius: 25px;
  position: absolute;
  right: 150px;
  width: 200px;
  height: 120px;
  padding: 14px 28px;
  font-color: white;
  cursor: pointer;
  background-color: #0096FF;
  border: none;
  padding: 15px 32px;
  text-align: center;
  display: inline-block;
  font-size: 36px;
  margin: 4px 2px;
  cursor: pointer;
  font-style: Brush Script MT;
}
<div id="btn" class="btn">Open</div>


<div id="myModal" class="modal">


  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Welcome</p>
  </div>

</div>

Upvotes: 1

Related Questions