1

first post here so please be gentle ;-)

I have a query about popup modals, that not only have an image within the modal, but some text too. Attached my current status of the coding, however could not figure out an easy way to get the text caption for each image. The image popups are correct, however text returned is TEXT1 for whichever I click.

Any help would be appreciated. Many thanks Sabs

function onClick(element) {
 document.getElementById("modal01").style.display = "block";
 document.getElementById("img01").src = element.src;
 document.getElementsByClassName("modal-content").innerHTML = this.alt;
}
@import url('https://fonts.googleapis.com/css?family=Varela+Round');

html, body {
    overflow-x: hidden;
    /*height: 100%;*/
}

body {
    background: url('../img/dark.jpg');
    background-size: cover;
    padding: 0;
    margin: 0;
    font-family: 'Varela Round', sans-serif;
}

.main {
    margin: 0 auto;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(6, 1fr); 
    grid-gap: 5px;
    margin-top: 100px;
    margin-right: 2rem;
    margin-left: 2rem;
}

.mainInner img{
    width: 100%;
    object-fit: cover;
      /*display:inline-block;*/
}

/* ---------------- placement of images within our grid system -----*/
.mainInner:nth-child(1){ grid-column: span 1; grid-row: span 1; }
.mainInner:nth-child(2){ grid-column: span 1; grid-row: span 1; }
.mainInner:nth-child(3){ grid-column: span 1; grid-row: span 1; }

.mainInner:nth-child(1):hover, .mainInner:nth-child(2):hover, .mainInner:nth-child(3):hover{ 
    transform: scale(1.05);
    cursor: pointer;
    transition: .5s;
    opacity: 0.30;
}

/*----------------------- styling the modal ------------------------*/
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 60px; /* 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.8); /* Black w/ opacity */
  /*styling the caption text*/
  color: #f1f1f1;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}

/* Modal Content */
.modal-content {
  margin: auto;
  display: block;
    /*position: absolute;*/
    /*transform: translate(-50%, -50%);*/
  width: 80%;
  max-width: 700px;
}

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {-webkit-transform:scale(0)} 
  to {-webkit-transform:scale(1)}
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

@media only screen and (max-width: 650px){
  .main{
        display: block;
    }
    .mainInner{
        margin-bottom: 1rem;
    }

  .modal-content {
    width: 100%;
  }
}

/*----------------- end modal styling -----------------*/
<!DOCTYPE html>
<html>

  <head>
    <title>Popup Modal Demo</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  </head>

  <body>

    <div class="main center">
      <div class="mainInner"><img id="myImg1" src="img/brands/AyalaBar.png" onclick="onClick(this)"></div>
      <div class="mainInner"><img id="myImg2" src="img/brands/BastianInverun.png" onclick="onClick(this)"></div>
      <div class="mainInner"><img id="myImg3" src="img/brands/breuning.png" onclick="onClick(this)"></div>
    </div>

    <!-- 1st IMAGE MODAL -->
    <div id="modal01" class="modal" onclick="this.style.display='none'">
      <div class="modal-content">
        <span class="close">&times;</span>
        <p>TEXT1</p>
        <img id="img01" style="max-width: 100%">
      </div>
    </div>

    <!-- 2nd IMAGE MODAL -->
    <div id="modal01" class="modal" onclick="this.style.display='none'">
      <div class="modal-content">
        <span class="close">&times;</span>
        <p>TEXT2</p>
        <img id="img01" style="max-width: 100%">
      </div>
    </div>

    <!-- 3rd IMAGE MODAL -->
    <div id="modal01" class="modal" onclick="this.style.display='none'">
      <div class="modal-content">
        <span class="close">&times;</span>
        <p>TEXT3</p>
        <img id="img01" style="max-width: 100%">
      </div>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="script2.js"></script>

  </body>

</html>
SABs
  • 11
  • 1
  • 3

1 Answers1

0

I'm a little bit late on this post - welcome to SO, Sabs! Kudos for making a first post with a clear and concise problem, and including your relevant code.

At first glance, the reason you're always returning "Text 1" is because your JS file hardcodes modal01 as the ID for each modal. So while the function is called whenever you click any of the images, it will always show that ID.

You don't have to re-invent the JS and CSS for a modal popup, though - a much simpler way is to use Bootstrap. You can still edit CSS and JS to your heart's content by giving IDs and classes, but you don't need much to call it. You need to include source CSS/JS files (from CDN here for simplicity's sake):

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

Calling the modal from your HTML uses a data-toggle and a data-target:

<div class="main center">
      <div class="mainInner"><img id="myImg1" src="img/brands/AyalaBar.png" data-toggle="modal" data-target="#myModal1"></div>
      <div class="mainInner"><img id="myImg2" src="img/brands/BastianInverun.png" data-toggle="modal" data-target="#myModal2"></div>
      <div class="mainInner"><img id="myImg3" src="img/brands/breuning.png" data-toggle="modal" data-target="#myModal3"></div>
</div>

The modals:

    <!-- Beginning Modal 1 -->
        <div class="modal fade" id="myModal1">
            <div class="modal-dialog">
                <div class="modal-content">

                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">Modal Heading 1</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>

                    <!-- Modal body -->
                    <div class="modal-body">
                        <p>The first modal..</p>
                        <img src="img/brands/AyalaBar.png">
                    </div>

                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    <!-- End Modal 1 -->

    <!-- Beginning Modal 2 -->
        <div class="modal fade" id="myModal2">
            <div class="modal-dialog">
                <div class="modal-content">

                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">Modal Heading 2</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>

                    <!-- Modal body -->
                    <div class="modal-body">
                        <p>A second modal..</p>
                        <img src="img/brands/BastianInverun.png">
                    </div>

                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    <!-- End Modal 2 -->

     <!-- Beginning Modal 3 -->
        <div class="modal fade" id="myModal3">
            <div class="modal-dialog">
                <div class="modal-content">

                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">Modal Heading 3</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>

                    <!-- Modal body -->
                    <div class="modal-body">
                        <p>The third modal..</p>
                        <img src="img/brands/breuning.png">
                    </div>

                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    <!-- End Modal 3 -->
Tee
  • 126
  • 6