I’m working on a website that uses a popup to display images when users click on certain elements. This website has 24 images, and when clicking on each image, it opens a different popup.
The current code allows me to display a single image in the popup when clicking on an element, but I need to add a second image to the same popup.
document.addEventListener('DOMContentLoaded', function() {
var div1 = document.querySelector('.spectra-image-gallery__media-wrppear');
var div2 = document.querySelectorAll('.spectra-image-gallery__media-wrapper')[1];
div1.addEventListener('click', function() {
openPopup('https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png', "https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png" );
});
div2.addEventListener('click', function() {
openPopup('https://media.istockphoto.com/id/475251515/vector/3d-shiny-red-number-2.jpg?s=612x612&w=0&k=20&c=oY4nzhBPaUUZ6OAudkWtWergdNrxD-G_8y4V4XAqSuQ=');
});
});
function openPopup(imageUrl) {
var popupContainer = document.getElementById('popupContainer');
var popupImage = document.getElementById('popupImage');
popupImage.src = imageUrl;
popupContainer.style.display = 'block';
}
function closePopup() {
var popupContainer = document.getElementById('popupContainer');
popupContainer.style.display = 'none';
}
I’m trying to add a second image inside the popup with the code below, but it doesn’t work. Is it possible to add something like this to just add a new addEventListener for each of the 24 images there and pass the src to each one of the images through the openPopup function?
document.addEventListener('DOMContentLoaded', function() {
var div1 = document.querySelector('.spectra-image-gallery__media-wrapper');
var div2 = document.querySelectorAll('.spectra-image-gallery__media-wrapper')[1];
div1.addEventListener('click', function() {
openPopup('https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png', 'https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png');
});
div2.addEventListener('click', function() {
openPopup('https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png', "https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png, https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png', "https://completeexteriorsms.com/wp-content/uploads/2023/06/residential-roofing-serices-1-e1688069648532.png');
});
});
function openPopup(imageUrl1, imageUrl2) {
var popupContainer = document.getElementById('popupContainer');
var popupImage = document.getElementById('popupImage');
var popupImage2 = document.getElementById('popupImage2');
popupImage.src = imageUrl1;
popupImage2.src = imageUrl2;
popupContainer.style.display = 'block';
}
function closePopup() {
var popupContainer = document.getElementById('popupContainer');
popupContainer.style.display = 'none';
}
Is it not possible to do something like this, is there any option to minimize the written code and avoid too much repetition?
2
Answers
To handle multiple images per popup in a dynamic way, you can modify your openPopup function to take an array of image URLs and dynamically create image elements for each URL.
Look at this code:
In this code, image URLs are placed inside an array, and map is used to generate HTML string for each image, then the innerHTML of popupContainer is replaced by these strings.
Hope this helps!
you can try this approach :
HTML Structure: Make sure your HTML structure has a common class for all the image elements you want to add this functionality to. For example:
html
Javascript :