Is there a way to prevent the content inside from opening before it is clicked? Every time I try to create a modal or dropdown list menu, the content always opens automatically before being clicked. Why does this happen?
var modal = document.getElementById('modal-wrap');
var open = document.getElementById('modal-open');
var close = document.getElementById('modal-close');
open.onclick = function() {
modal.style.display = "block";
};
close.onclick = function() {
modal.style.display = "none";
};
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
.modal-wrap {
display: none;
position: fixed;
z-index: 99;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
opacity: 40%;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.modal-close {
float: right;
padding: 5px;
border-radius: 5px;
font-weight: bold;
}
.modal-close:hover,
.modal-close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
<button id="modal-open">Open</button>
<div id="modal-wrap" class="modal-wrap">
<div class="modal-content">
<button class="modal-close" id="modal-close">Close</button>
<p>Edit the data </p>
<div class="form-group">
<label for="name">Name</label>
<input id="name" placeholder="Input your Name">
</div>
</div>
</div>
Furthermore, this code works as expected in snippets or JS Bin. However, why does its appearance change by default when running it on my localhost project? here