I have a ton of modals on a page and I'm using this script to link the ID of an HTML button to the ID of the modal and it's close button:
var target = document.getElementById('show1');
var currentOpacity = 0;
$(document).ready(function () {
$("#hide1").click(function () {
$("#details1").hide();
target.style.opacity = currentOpacity + 1;
});
$("#show1").click(function () {
$("#details1").show();
target.style.opacity = currentOpacity - 1;
});
});
const button1 = document.querySelector("#show1");
const readout1 = document.querySelector("p");
button1.addEventListener("mousemove", (e) => {
const {
x,
y
} = button1.getBoundingClientRect();
button1.style.setProperty("--x", e.clientX - x);
button1.style.setProperty("--y", e.clientY - y);
});
This works but I have 56 buttons... How could I write this script so that #show1 #details1 #hide1, #show2 #details2 #hide2, #show3 #details3 #hide3 etc. all the way to 56 work without duplicating this code block 56 times?
EDIT
Here is the HTML for an item:
<section id="photo01" class="item-module-wrap">
<div class="item-module">
<div class="blurred-image" style="background-image: url('xxxxx');"></div>
<div class="dot-texture"></div>
<article>
<div class="dgov-grid">
<div class="dgov-col-sm-7 dgov-col-md-8 dgov-col-lg-9">
<div class="main-photo-module">
<div class="sticky">
<h2>xxxxx</h2>
</div>
<div class="sticky"><span>xxxxx</span></div>
<div class="sticky">
<button type="button" id="show1" class="details-button shiny">Details</button>
</div>
<img src="xxxxx" alt="xxxxx"/> </div>
</div>
<div class="dgov-col-sm-5 dgov-col-md-4 dgov-col-lg-3">
<div class="aside-module">
<div class="details" id="details1">
<div class="close" id="hide1"><i class="fa fa-times" aria-hidden="true"></i></div>
<p class="description">xxxxx</p>
<p class="extra">xxxxx</p>
<p class="extra">xxxxx</p>
<div class="social">
xxxxx
</div>
</div>
<div class="sticky">
<time> 1 of 56 </time>
</div>
<div class="item-number layer" data-px-per-scroll="0.5" data-initial-position="450">01</div>
</div>
</div>
</div>
</article>
</div>
</section>
HTMLstructure for a single module ?