Giacomo Benicchi
Giacomo Benicchi

Reputation: 21

how can I make a single script for all the modal?

On the same page, I have about a dozen images and on each image at the click you open a modal. I made a script for each modal, how can I make a single script for all the modal?

<!-- 1 Modal-->
<div class="gallery_product col-lg-3 col-md-3 col-sm-3 col-xs-3 filter mercoledì ombra">
<img onclick="myLele(this)" src="https://www.festadellamusicact.it/wp-content/uploads/leletronika.jpg" id="myImg" class="img-responsive"></div>
<div id="lele" class="modal">
<div class="modal-content" id="img11">
<span onclick="undici.style.display = 'none'" class="close">&times;</span>
<img src="https://www.festadellamusicact.it/wp-content/uploads/33407528_10216104175664929_3838668853781463040_n.jpg" class="img-responsive img-modale"></div>
</div> 
<!-- 2 Modal-->
<div class="gallery_product col-lg-3 col-md-3 col-sm-3 col-xs-3 filter mercoledì ombra">
<img onclick="myJessy(this)" src="https://www.festadellamusicact.it/wp-content/uploads/jessy.jpg" id="myImg" class="img-responsive"></div>
<div id="jessy" class="modal">
<div class="modal-content" id="img10">
<span onclick="dieci.style.display = 'none'" class="close">&times;</span>
<img src="https://www.festadellamusicact.it/wp-content/uploads/29543_497687346938913_28179288_n.jpg" class="img-responsive img-modale">
<script>
   var undici = document.getElementById('lele');
   var lele = document.getElementById("img11");

function myLele(el) {
        var ImgSrc = el.src;
        undici.style.display = "block";
        lele.src = ImgSrc;
    }
    window.onclick = function (event) {
        if (event.target == undici) {
            undici.style.display = "none";
        }
    }
</script>
<script>
    var dieci = document.getElementById('jessy');
    var jessy = document.getElementById("img10");

function myJessy(el) {
        var ImgSrc = el.src;
        dieci.style.display = "block";
        jessy.src = ImgSrc;
    }
    window.onclick = function (event) {
        if (event.target == dieci) {
            dieci.style.display = "none";
        }
    }
</script>

I tried different ways, but not on multiple modals on the same page does not work. Can I do a foreach () loop?

Upvotes: 2

Views: 55

Answers (1)

AndrewL64
AndrewL64

Reputation: 16321

IDs should be unique for each element. Change the IDs of both your images from myImg to something unique like myImg1 and myImg2 respectively.

Also, you don't need to write a custom function for toggling your modals. Just use the in-build modals in Bootstrap by adding data-toggle="modal" data-target="#lele" to your modal 1 image and data-toggle="modal" data-target="#jessy" to your modal 2 image like this:

<img src="/leletronika.jpg" id="myImg1" class="img-responsive" data-toggle="modal" data-target="#lele">
<img src="/jessy.jpg" id="myImg2" class="img-responsive" data-toggle="modal" data-target="#jessy">

You also need to add the data-dismiss attribute to your close button like this:

<span class="close" data-dismiss="modal">&times;</span>

Check and run the following Code Snippet for a practical exmaple of what I have described above:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>

<!-- 1 Modal-->
<div class="gallery_product col-lg-3 col-md-3 col-sm-3 col-xs-3 filter mercoledì ombra">
  <img src="https://www.festadellamusicact.it/wp-content/uploads/leletronika.jpg" id="myImg1" class="img-responsive" data-toggle="modal" data-target="#lele">
</div>
<div id="lele" class="modal">
  <div class="modal-content" id="img11">
    <span class="close" data-dismiss="modal">&times;</span>
    <img src="https://www.festadellamusicact.it/wp-content/uploads/33407528_10216104175664929_3838668853781463040_n.jpg" class="img-responsive img-modale">
  </div>
</div> 
<!-- 2 Modal-->
<div class="gallery_product col-lg-3 col-md-3 col-sm-3 col-xs-3 filter mercoledì ombra">
  <img src="https://www.festadellamusicact.it/wp-content/uploads/jessy.jpg" id="myImg2" class="img-responsive" data-toggle="modal" data-target="#jessy">
</div>
<div id="jessy" class="modal">
  <div class="modal-content" id="img10">
    <span class="close" data-dismiss="modal">&times;</span>
    <img src="https://www.festadellamusicact.it/wp-content/uploads/29543_497687346938913_28179288_n.jpg" class="img-responsive img-modale">
  </div>

Upvotes: 1

Related Questions