Looking to replicate a Modal that I designed, but unsure which elements need altering in order to achieve this. I am hoping to create three duplicates of the Modal

This modal is functioning perfectly, and now I want to replicate the same modal three times on a single page. I require three distinct buttons on the same page to trigger these separate modals. At this point, I am unsure which attributes need modification in order to accomplish this.

Below is the code within the body (CTA Button):

<!-- Trigger the modal with a button -->
<button id="modal-btn" class="button"> LEARN MORE</button>

This is the code for the Modal...

<!-- JQUERY SCRIPT -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- BEGIN CSS STYLES -->
<style>
.modal {
  display: none;
  position: fixed; 
  padding-top: 50px;
  left: 0; 
  top: 0;
  width: 100%;
  height: 100%; 
  z-index: 100;
  background-color: white;
  background-color: rgba(0, 0, 0, 0.5);
  overflow: scroll;
}
.modal-content {
  position: relative; 
  padding: 10px; 
  margin: auto; 
  width: 75%;  
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s;
}
.close-btn {
  float: right;
  padding-right: 50px; 
  color: lightgray; 
  font-size: 24px;  
  font-weight: bold;
}
.close-btn:hover {
  color: darkgray;
}
@-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}
}

.button {
  background-color: #04347b; /* Blue */
  border: none;
  color: white;
  padding: 12px 80px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}
</style>
<!-- END CSS STYLES -->

<!-- BEGIN MODAL -->
<div class="modal">
<div class="modal-header">
    <span class="close-btn">&times;</span>
  </div>
  
<div class="modal-content">
<div class="col-sm-12 col-md-12" style="background-color: white; padding: 25px 50px;">
<div class="col-sm-12 col-md-12"><h2>Physiotherapy</h2></div>
<!--PRODUCT 1-->
<div class="col-sm-2 col-md-2" style="padding:10px 0;"><img src="{{media url=wysiwyg/images-test-1.png}}" alt="" /></div>
<div class="col-sm-4 col-md-4" style="padding:10px 0; text-align: left;">
<p><strong>PRODUCT TITLE 1<br>
Ref: xxxxxxx</strong></p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<!--END PRODUCT 1-->
...
</div>

</div>
</div>
<!-- END MODAL -->

<!-- BEGIN SCRIPT -->
<script type="text/javascript">
let modalBtn = document.getElementById("modal-btn")
let modal = document.querySelector(".modal")
let closeBtn = document.querySelector(".close-btn")
modalBtn.onclick = function(){
  modal.style.display = "block"
}
closeBtn.onclick = function(){
  modal.style.display = "none"
}
window.onclick = function(e){
  if(e.target == modal){
    modal.style.display = "none"
  }
}
</script>
<!-- END SCRIPT -->

<!-- BEGIN SCRIPT -->
<script type="text/javascript">
$('.modal').click(function(){
  $('.modal').show();
  $('body').css("overflow", "hidden");
});
</script>
<!-- END SCRIPT -->

Answer №1

If you want to keep the content hidden, you can store it in a hidden div like so:

<div class="hidden-content-1" style="display:none">CONTENT 1</div>
<div class="hidden-content-2" style="display:none">CONTENT 2</div>
<div class="hidden-content-3" style="display:none">CONTENT 3</div>

Your modal setup should look like this:

<div class="modal">
<div class="modal-header">
    <span class="close-btn">&times;</span>
</div>
<div class="modal-content"></div>
</div>

Include buttons for each item:

<button onclick="openModal('1')" class="button"> LEARN MORE 1</button>
<button onclick="openModal('2')" class="button"> LEARN MORE 2</button>
<button onclick="openModal('3')" class="button"> LEARN MORE 3</button>

The accompanying JavaScript function should be as follows:

<script>
function openModal(i) {
  $('.modal-content').html($('.hidden-content-'+i).html())
  $('.modal').show();
  $('body').css("overflow", "hidden");
}
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript truthy values referring to numbers

According to the rules outlined below: Falsy: false 0 (zero) '' or "" (empty string) null undefinded NaN (e.g. the result of 1/0) Truthy: Anything else I'm puzzled as to why, in the tests that follow, only the number 1 is considered "tr ...

Transitioning to Material-ui Version 4

During the process of upgrading material-ui in my React Application from version 3.9.3 to version 4.3.2, I encountered an error message stating TypeError: styles_1.createGenerateClassName is not a function. I am feeling lost when it comes to transitioning ...

Utilize a function or array to send various data with an Ajax post request

Hey, I'm on the hunt for a more efficient method to send data using ajax to php for multiple users. Take a peek at my code below: $(document).ready(function(){ $("#all").click(function(){ document.getElementById('babon').click(); ...

Converting a numerical value starting with zero into a string while retaining the leading zero

I am facing an issue with my JavaScript code that retrieves GPS location from a browser. Due to the limitation of writing coordinates to an SQL database directly from JavaScript, I have resorted to sending it via PHP by utilizing the controller "site" and ...

The initial ajax request successfully connects to the file, but encounters a 404 error upon the second attempt

I am currently encountering an issue with a jQuery post function. The function is designed to run multiple times and then stop, which it has successfully done in the past. However, now the problem arises when the function tries to execute itself again afte ...

What advantages and disadvantages come with choosing between using vh and vw for responsive text?

My main inquiry is centered around the various techniques for creating responsive text. Is there a particular method that stands out as the best option for general use, or do different methods serve distinct purposes? ...

The issue arises when d3.scaleLinear returns NaN upon the second invocation

My journey with d3.js is just beginning and I'm taking it slow. Currently, I'm focused on creating a bar chart where data is loaded from a json file. When I click on the bars, the data changes to another column in the json. This is how my json f ...

Massive Memory Drain Due to XMLHttp POST Request

Is there a way to prevent XHR POST Memory leak in my web app? I have searched extensively for solutions but have not found any satisfactory answers. My issue is similar to the one described in this blog post that outlines the problem without offering any f ...

How can I create a script for a sliding/toggling menu?

Not sure if it's appropriate to ask, but I'm currently in search of a slide/toggle menu. Despite my efforts on Google, I haven't been able to find exactly what I need. As someone who is more skilled in HTML/CSS than jQuery or other scripting ...

The AJAX function is failing to return false

I found this code snippet in my index.html file: <form> <input onkeyup="firstnamecheck(this.value)" type="text" name="firstname" id="Start" class="Inputs" /> <button type="submit" name="Murad" id="tester" title="Register">Register</b ...

Issue with Jquery UI sortables not functioning properly

Struggling with getting a sortable list to work using jQuery UI. The ul element is receiving the class 'ui-sortable' but nothing seems to be happening. To demonstrate this issue, I created an example since the original javascript code is quite c ...

A Guide to Implementing Inner CSS in Angular

I am working with an object named "Content" that has two properties: Content:{ html:string; css:string } My task is to render a div based on this object. I can easily render the html using the following code: <div [innnerHtml]="Content.html"& ...

Exploring Angular 2 with ng-bootstrap Library

As I delve into learning Angular2, my goal is to incorporate ng-bootstrap into my project. However, I have encountered issues when trying to import ng-bootstrap and create a functional project. Being a novice in this field, I am unsure if the problem lies ...

Spin a sphere by clicking on a link using three.js

I've designed a spherical object and have a group of links. I'm looking for a way to manipulate the position or rotation of the sphere by simply clicking on one of the links. Despite extensive searching, I haven't been able to find an exampl ...

The jspdf tool tries to cram my extensive data into a single page, resulting in an overcrowded and barely visible PDF document

My PDF generated using Jspdf is being forced to fit in one page, making it difficult to see all the data because there is a huge amount of information present. To view the issue, please visit this link: https://jsfiddle.net/frost000/04qt7gsm/21/ var pdf ...

Store additional data with the visitor ID WEB in Fingerprint JS V3

After browsing through similar questions, I couldn't find a solution that fits my needs. Basically, I have a website where a random emoji is generated and stored in local storage. However, this method is not effective as users can easily delete their ...

Instructions on inserting an IFRAME using JavaScript into dynamically loaded content via AJAX

How can I dynamically add an IFRAME using JavaScript to content that is refreshed via AJAX? Consider the following example: $('#bar').delegate('.scroll-content-item span a', 'click', function() { var object_id = $(this).p ...

The Ajax request returned empty data

Is there a problem with retrieving data based on user selection from a drop-down menu in an MVC 5 controller? I am able to capture the selected value, but when passing it to the controller, the data appears to be null. Any suggestions or solutions? jQuery ...

Implementing styles from a constant file in React Native: A simple guide

In my file register.js, I have a UI component. import CustomHeader from '../components/Header'; ... static navigationOptions = ({navigation, navigation: { state } }) => { return { title: '', headerSty ...

The for loop GET request is not successfully pushing data to MongoDB, leaving the database with no entries

My current challenge lies in transmitting data from my for loop to MongoDB. Upon executing the js file using node initCount.js in the console, no errors are returned and everything seems to be working correctly. However, upon checking my MongoDB backend, I ...