Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I HAVE THESE TWO DIVERSE MODALS, WHICH WORK PERFECTLY WELL ON SEPARATE PAGES, BUT I WANT THEM TO FUNCTION ON THE SAME PAGE. EACH OTHER PERFORM DIFFERENT TASKS. COULD ANYBODY HELP ME ADJUST THEM ACCORDINGLY? I'M BEGINNING TO DWELL ON THE WORLD OF PROGRAMMING. I'D REALLY  APPRECIATE YOUR ASSISTANCE.

THE FIRST ONE:



<html>


<head>
    

<style>
  
  
  
/* 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 {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

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

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}


</style>
  
  
  
  
  
</head>

  
  
  
  
  
<body>
  
  
  

  
<h2>1st Modal</h2>

<!-- Trigger/Open The Modal -->
<button class="modal-button" href="#myModal1">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      ×
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

<h2>2nd Modal</h2>

<!-- Trigger/Open The Modal -->
<button class="modal-button" href="#myModal2">Open Modal</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      ×
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>



<script>

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

// All page modals
var modals = document.querySelectorAll('.modal');

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

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
 btn[i].onclick = function(e) {
    e.preventDefault();
    modal = document.querySelector(e.target.getAttribute("href"));
    modal.style.display = "block";
 }
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
 spans[i].onclick = function() {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
    }
 }
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target.classList.contains('modal')) {
     for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
     }
    }
}


  
  
</script>

</body>

</html>



AND THE OTHER ONE:




<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

body {font-family: Arial, Helvetica, sans-serif;}



#myImg {

  border-radius: 5px;

  cursor: pointer;

  transition: 0.3s;

}



#myImg:hover {opacity: 0.7;}



/* 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.9); /* Black w/ opacity */

}



/* Modal Content (image) */

.modal-content {

  margin: auto;

  display: block;

  width: 80%;

  max-width: 700px;

}



/* Caption of Modal Image */

#caption {

  margin: auto;

  display: block;

  width: 80%;

  max-width: 700px;

  text-align: center;

  color: #ccc;

  padding: 10px 0;

  height: 150px;

}



/* 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;

}



/* 100% Image Width on Smaller Screens */

@media only screen and (max-width: 700px){

  .modal-content {

    width: 100%;

  }

}

</style>

</head>



<body>



<h2>Image Modal</h2>

<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>

<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>



<img id="myImg" src="img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">



<!-- The Modal -->

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

  ×

  <img class="modal-content" id="img01">

  <div id="caption"></div>

</div>



<script>

// Get the modal

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



// Get the image and insert it inside the modal - use its "alt" text as a caption

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;

}



// Get the <span> element that closes the modal

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



// When the user clicks on <span> (x), close the modal

span.onclick = function() { 

  modal.style.display = "none";

}

</script>



</body>

</html>


What I have tried:

Since I am new in the field of programming, I am unable to figure out how to make both modals work on same page. I think that the answer has to include the Class feature of CSS.
Posted
Comments
gggustafson 3-May-21 15:05pm    
Are you sure you understand what "modal" means? If one modal dialog is correctly implemented and open, then another modal dialog must wait for the first to close. That's what modal is all about. You cannot leave a modal dialog without completing its requirements. So although you might have two modal dialogs on a page, the user can only access one modal dialog at a time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900