Hi @javascript7, I haven’t tried it but that’s probably because the modal ID is getting acquired from the event target:
var id = event.target.getAttribute('data-modal')
However if you’re clicking an image inside the link, the event target will refer to that image, not the link; and the image doesn’t have the data-modal attribute. You can solve this by getting the ID from the event.currentTarget instead (which is the element the event listener got added to rather than the element from which the event got dispatched):
Not silly! But yes, the path to the image is correct because I can see it on the page. Like I mentioned, if it’s text it’s fine and if I replace with an image, it looks like this:
Good idea Paul, thank you. This is one of the modal that you worked on that has been very useful for me. You did an incredible job on it. There were 2 version and maybe I am getting them mixed up. This one has the iframe-wrap.
Here’s the JS:
window.onload = function () {
var openModalLinks = Array.prototype.slice.call(document.querySelectorAll('[data-open-modal]'));
var closeModal = document.querySelector('.modal-close');
var modalOverlay = document.querySelector('.modal-overlay');
var modalInner = document.querySelector('.modal-inner');
var modalContent = document.querySelector('.modal-inner');
var modalContent = Array.prototype.slice.call(document.querySelectorAll('div[class^="modal-content-"]'));
var handleShowModal = function (id) {
document.querySelector('body').classList.add('modal-visible');
modalOverlay.classList.add('visible');
modalOverlay.querySelector('.modal-content-' + id).classList.remove('hide');
modalOverlay.querySelector('.modal-content-' + id).classList.add('isPlaying');
}
var handleHideModal = function (event) {
modalOverlay.classList.remove('active');
}
var handleOpenModal = function (event) {
var id = event.target.getAttribute('data-modal');
event.preventDefault();
modalOverlay.classList.add('active');
window.setTimeout(handleShowModal(id));
}
var handleCloseModal = function (event) {
if (event.target !== event.currentTarget) {
return;
}
var el = document.querySelector('.isPlaying iframe');
event.preventDefault();
document.querySelector('body').classList.remove('modal-visible');
if (el) {
el.setAttribute("src", el.getAttribute("src"));
}
modalOverlay.classList.remove('visible');
modalContent.forEach(function (modal) { modal.classList.add('hide'); modal.classList.remove('isPlaying') });
modalOverlay.addEventListener(
'transitionend',
handleHideModal, {
once: true,
passive: true
}
);
}
openModalLinks.forEach(function (link) { link.addEventListener('click', handleOpenModal) });
closeModal.addEventListener('click', handleCloseModal);
modalInner.addEventListener('click', handleCloseModal);
}
Well now I know - I tried it again and again it didn’t work. So then I thought that perhaps the old version was caching on my computer. Found out that all browsers including the cell phone, that once the cache was cleared on each browser, it worked!
So thank you to all and sorry that I hadn’t of thought about the cache in the beginning. I would like to be educated and know why it had to be changed. I thought that in using a href that whether it’s text or an image it should always work. Why is this different?