Ad

My JavaScript Modal Won't Open From The Trigger Button

- 1 answer

I wrote and copied a small piece of JavaScript for a modal and added it to my main.js file however it doesn't seem to work.

it works on jsfiddle but not on my machine: https://jsfiddle.net/281wr3c6/13/

it just doesn't seem to on my machine for some reason

var modal = document.getElementById("theModal");
var btn = document.getElementById("trigger");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
  modal.style.display = "block";
}
div.onclick = function() {
  modal.display.style = "none";
}
window.onclick = function(event) {
  if (event.target == modal) {
    modal.display.style = "none";
  }
}
.modal {
    display: none;
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background: rgba(11,12,12,0.66);
}

.modal.bg {
    padding:30px;
    float: left;
    z-index: 0;
    border: none;
    border-radius: 0;
    margin: 18px 0;
}

.modal .card {
    border: 4px solid #dee0e2;
    padding:24px;
    float: left;
    margin: 18px 0;
    z-index: 10;
    background: #fff;
}

.modal .title {
    width: 90%;
    display: inline-block;
}

.modal h3 {
    margin-top:0;
}

.modal div.buttons button.btn {
    margin-bottom: 0!important;
}

.modal div.buttons button.btn.secondary {
    margin-left: 0!important;
    float: left;
}

.modal div.buttons button.btn.primary {
    margin-right: 0!important;
    float: right;
}

.modal div.close {
    background-image: url("../assets/icons/close.svg");
    display: inline-block;
    height: 18px;
    width: 18px;
    padding: 3px;
    float: right;
}
<button id="trigger" class="btn primary">Open modal</button>
<div id="theModal" class="cd-box modal bg">
  <div class=card>
    <div class="title">
      <h3>Title</h3>
    </div>
    <div class="close"></div>
    <div class="inside">
      <p>This is some content that goes into the modal. The width adjusts according to the content.</p>
    </div>
    <div class="buttons">
      <button class="btn secondary">Exit</button>
      <button class="btn primary">Continue with action</button>
    </div>
  </div>
</div>

I got the code from here: https://www.w3schools.com/howto/howto_css_modals.asp

When I run my code in their fiddle/preview it does work so leads me to think that it's something that my site isn't running?

Ad

Answer

I have made a number of modifications to your code and it is now working in this JSFiddle:

https://jsfiddle.net/9ajsnx21/13/

Javascript

var modal = document.getElementById("theModal");
var btn = document.getElementById("trigger");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
    modal.style.display = "block";
}

span.onclick = function() {
    modal.style.display = "none";
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.display.style = "none";
  }
}

Html

<button id="trigger" class="btn primary">Open modal</button>
<div id="theModal" class="cd-box modal bg">
  <div class="modal-content">
    <div class="title">
      <h3>Title</h3>
    </div>
    <span class="close">&times;</span>
    <div class="inside">
      <p>This is some content that goes into the modal. The width adjusts according to the content.</p>
    </div>
    <div class="buttons">
      <button class="btn secondary">Exit</button>
      <button class="btn primary">Continue with action</button>
    </div>
  </div>
</div>

CSS

body {font-family: Arial, Helvetica, sans-serif;}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.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;
}
  • The javascript you were using was mostly fine, I changed "div" to "span".

  • The main issue was that you do not appear to have included any of the styling. Including the styling from the example you provided allowed it to work.

Ad
source: stackoverflow.com
Ad