Home Creating a modal for multiple images
Reply: 0

Creating a modal for multiple images

user4497 Published in March 21, 2018, 8:20 pm

Hello I'm trying to find a solution to having a modal appear when I click on an image on a carousel. I have done this for one of my images however I want to use this same bit of code for around 20 images. Is there a way to do this without copying the code for each image? This is using bootstrap but I dont think it is needed to be able to do this, thanks.

    <html lang="en">

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/styles.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>




    <div class="container" style="padding-bottom:15px;">
         <div class="row"> 
             <div class="col-2"></div>
             <div class="col-8">
                 <h2>Carousel Example</h2> 
                  <div id="myCarousel" class="carousel slide" data-ride="carousel">
                    <!-- Indicators -->
                    <ol class="carousel-indicators">
                      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                      <li data-target="#myCarousel" data-slide-to="1"></li>
                      <li data-target="#myCarousel" data-slide-to="2"></li>

                    <!-- Wrapper for slides -->
                    <div class="carousel-inner">
                      <div class="item active">
                        <img src="Photos/Buttons/BridgeNON.png" alt= "Bridge" class="img‐fluid" onmouseover="this.src='Photos/Buttons/Bridge.png'" onmouseout="this.src='Photos/Buttons/BridgeNON.png'" style="width:100%;">

                      <div class="item">
                        <img id="myImg" src="Photos/Buttons/waterNON.png" alt= "Water" class="img‐fluid" onmouseover="this.src='Photos/Buttons/water.png'" onmouseout="this.src='Photos/Buttons/waterNON.png'" style="width:100%;">

                      <div class="item">
                        <img src="" alt="" style="width:100%;">

                    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                      <span class="glyphicon glyphicon-chevron-left"></span>
                      <span class="sr-only">Previous</span>
                    <a class="right carousel-control" href="#myCarousel" data-slide="next">
                      <span class="glyphicon glyphicon-chevron-right"></span>
                      <span class="sr-only">Next</span>
             <div class="col-2"></div>

    <div id="myModal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="img01">
        <div id="caption"></div>

var modal = document.getElementById('myModal');

var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;

var span = document.getElementsByClassName("close")[0];

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.29294 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO