0

Dears, When I run this code in wordpress it is not working as should. Thing is that I want to by clicking on each pictures is caused modal showing with text about a person on this picture. This should looks like - three pictures in three columns, click and you can read about person on the picture.

Besides, when I scroll down or up the page, modal is going behind other pics...

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

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

// 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 {
    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: 70%;
}

/* 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;
}
<div id="myModals"><img class="scale-with-grid" src="http://halfway.devil.org.pl/wp-content/uploads/2018/11/MY-BRIGHTEST-DIAMOND-artist.jpg" alt="" /></div>
<div id="myModals"><img class="scale-with-grid" src="http://halfway.devil.org.pl/wp-content/uploads/2018/11/MY-BRIGHTEST-DIAMOND-artist.jpg" alt="" /></div>
<div id="myModals"><img class="scale-with-grid" src="http://halfway.devil.org.pl/wp-content/uploads/2018/11/MY-BRIGHTEST-DIAMOND-artist.jpg" alt="" /></div>
<div id="myModals"><img class="scale-with-grid" src="http://halfway.devil.org.pl/wp-content/uploads/2018/11/MY-BRIGHTEST-DIAMOND-artist.jpg" alt="" /></div>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p><h3>MY BRIGHTEST DIAMOND</h3>
LOREM IPSUM</p>
  </div>

</div>
  • I edited the post now, sorry... – Iza Adamska Nov 23 '18 at 12:05
  • One suggestion -- use to detect outside click { event.target !== modal } – Diljohn5741 Nov 23 '18 at 12:20
  • @Diljohn5741 That would close the modal also on a click on some element inside the modal. You'd need something like `(!modal.contains(e.target))` ... – Teemu Nov 23 '18 at 12:53
  • Do you want to have one modal for each picture or just one modal for all and change its content with JavaScript? There are pics overlapping your modal... do they have a higher `z-index`? how are they positioned (absolute, fixed, ...)? Check [this answer](https://stackoverflow.com/a/25476804/5247200). – David Nov 23 '18 at 15:02
  • Regarding having the pictures in different columns, are you using any front-end framework like Bootstrap? If not, read [this article](https://css-tricks.com/guide-responsive-friendly-css-columns/). – David Nov 23 '18 at 15:08

1 Answers1

-1

you are used id selectors and id is always unique in HTML dom, we don't use like duplication.

you need to change selector as a class and implement onclick function on that.

like

<div class="img-div"><img class="scale-with-grid" src="http://halfway.devil.org.pl/wp-content/uploads/2018/11/MY-BRIGHTEST-DIAMOND-artist.jpg" alt="" /></div>

var btn = document.getElementsByClassName('img-div');

btn.onclick = function() {
    modal.style.display = "block";
}
Hiren Ghodasara
  • 336
  • 1
  • 9