1

I am pretty much new to the coding world, specially when it comes to using JS or jQuery.

I have found online how to open a modal to display information. I am currently working on a page where, when you click on an image, it displays a modal with the information of the product.

I have been able to make one button work displaying information, but when I trigger the second button, it makes both buttons display the same information.

I can't see where I'm going wrong.

Here is the code I'm using.

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

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

// 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";
  }
}



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

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

// 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";
  }
}
.boton {
  border: solid 3px black;
  padding: 3px 6px;
}

.productname {
  font-size: 20px;
}

.imagedesktop {
  width: 45%;
  float: left;
}


/* 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%;
  margin-top: 110px;
  height: 680px;
}


/* 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;
}
<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- Trigger/Open The Modal -->
<button id="modaltwo">Open Modal Two</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <h2 class="productname">Tshirt</h2>
    <div>
      <img src="https://i3.cpcache.com/product/178522355/funny_choir_womens_dark_tshirt.jpg?width=550&height=550&Filters=%5B%7B%22name%22%3A%22background%22%2C%22value%22%3A%22F2F2F2%22%2C%22sequence%22%3A2%7D%5D" class="imagedesktop" />
    </div>
    <div>
      <p>This is the first modal description.</p>
    </div>
    <div>
      <span class="boton"><a href="http://google.com">SHOP NOW</a></span>
    </div>
  </div>

</div>

<!-- The Modal TWO-->
<div id="mymodalTWO" class="modal">
  <!-- Modal content TWO-->
  <div class="modal-content">
    <span class="close">&times;</span>
    <h2 class="productname">Tshirt TWO</h2>
    <div>
      <img src="https://cdn.shopify.com/s/files/1/1391/7881/products/spiderman_homecoming_shirt_front_1024x1024.jpg?v=1500006303" class="imagedesktop" />
    </div>
    <div>
      <p>This is the second modal description.</p>
    </div>
    <div>
      <span class="boton"><a href="http://google.com">SHOP NOW!</a></span>
    </div>
  </div>

</div>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
MFB
  • 105
  • 1
  • 1
  • 6

1 Answers1

3

You are using same variable names for both modals. you need to change variable names in order to store different information in it.

As you mentioned that you are beginner in JS, you should read more about scoping, this post will help you.

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

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

// 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";
  }
}



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

// Get the button that opens the modal
var btn2 = document.getElementById("modaltwo");

// Get the <span> element that closes the modal
var span2 = document.getElementsByClassName("close")[1];

// When the user clicks the button, open the modal 
btn2.onclick = function() {
  modal2.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span2.onclick = function() {
  modal2.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal || event.target == modal2) {
    modal.style.display = "none";
    modal2.style.display = "none";
  }
}
.boton {
  border: solid 3px black;
  padding: 3px 6px;
}

.productname {
  font-size: 20px;
}

.imagedesktop {
  width: 45%;
  float: left;
}


/* 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%;
  margin-top: 110px;
  height: 680px;
}


/* 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;
}
<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- Trigger/Open The Modal -->
<button id="modaltwo">Open Modal Two</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <h2 class="productname">Tshirt</h2>
    <div>
      <img src="https://i3.cpcache.com/product/178522355/funny_choir_womens_dark_tshirt.jpg?width=550&height=550&Filters=%5B%7B%22name%22%3A%22background%22%2C%22value%22%3A%22F2F2F2%22%2C%22sequence%22%3A2%7D%5D" class="imagedesktop" />
    </div>
    <div>
      <p>This is the first modal description.</p>
    </div>
    <div>
      <span class="boton"><a href="http://google.com">SHOP NOW</a></span>
    </div>
  </div>

</div>

<!-- The Modal TWO-->
<div id="mymodalTWO" class="modal">
  <!-- Modal content TWO-->
  <div class="modal-content">
    <span class="close">&times;</span>
    <h2 class="productname">Tshirt TWO</h2>
    <div>
      <img src="https://cdn.shopify.com/s/files/1/1391/7881/products/spiderman_homecoming_shirt_front_1024x1024.jpg?v=1500006303" class="imagedesktop" />
    </div>
    <div>
      <p>This is the second modal description.</p>
    </div>
    <div>
      <span class="boton"><a href="http://google.com">SHOP NOW!</a></span>
    </div>
  </div>

</div>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
  • Can I also ask you why is the X to close the modal not working? Is there a way to close it? Thanks in advance! – MFB Jan 18 '18 at 15:05
  • `span2` is array of classes, `span2` was selecting first array with `document.getElementsByClassName("close")[0];` I have changed it to `[1]`, it is working now. – Abhishek Pandey Jan 19 '18 at 05:45
  • 1
    Thank you so much, you truly are amazing! Really really appreciate your help! Have an amazing day! – MFB Jan 19 '18 at 08:16