I am having small issue. I am trying to replace my src=#
attribute that i have in my html, with replace()
but I am always getting my inital src.
I am trying do display clicked photo in my modal. Target photo is my inital source for src
.
My html looks like this:
<section id="gallery" class="Gallery">
<div id="photo" class="Gallery__Container">
<div class="gallery-modal">
<img class="modal-image" src="#" alt="gallery_modal" />
</div>
<figure class="Gallery__Item">
<img
class="Gallery__Photo"
loading="lazy"
src="../assets/gal_1.jpg"
alt="gallery"
data-src="../assets/gal_1.2a8a9bc795a316356b7fb8363fe0f25f.jpg"
/>
</figure>
<figure class="Gallery__Item">
<img
class="Gallery__Photo"
loading="lazy"
src="../assets/gal_2.jpg"
alt="gallery"
/>
</figure>
<figure class="Gallery__Item">
<img
class="Gallery__Photo"
loading="lazy"
src="../assets/gal_3.jpg"
alt="gallery"
/>
</figure>
</div>
</section>
I have bunch of photos that have my initial src
And i have my modal-image
with src=#
What I was trying is looking like this:
/* get selectors */
const photoGal = document.getElementById('photo');
const galPhotos = document.querySelectorAll('.Gallery__Photo');
const galModal = document.querySelector('.gallery-modal');
const galModalImg = document.querySelector('.modal-image');
/* add listener and replace src */
const addModalCalss = (e) => {
let data;
for (let photo of galPhotos) {
data = photo.dataset;
// const photoSrc = galModalImg.getAttribute('src');
// galModalImg.replace('src="#"', 'src=' + data.src);
// if (galModalImg.src === undefined) {
// galModalImg.toString().replace('src="#"', 'src=' + data.src);
// }
// console.log(data.src);
// console.log(galModalImg.src, 'gal src');
// console.log(photoSrc);
// galModalImg.toString().replace('src="#"', 'src=' + data.src);
// console.log(data.src, 'src');
console.log(galModalImg.getAttribute('src'), 'html img');
console.log(photo.dataset.src, 'photo');
galModalImg.toString().replace('src="#"', 'src=' + data.src);
console.log(galModalImg.getAttribute('src'), 'html img 2');
}
console.log(data.src, 'src');
// galModalImg.toString().replace('src="#"', 'src=' + data.src);
// galModal.classList.add('modal-active');
};
for (let galImg of galPhotos) {
galImg.addEventListener('click', addModalCalss);
}
I want to use dataset
. Idea is simple and not sure why this is not working. I am looping through all photos and setting data
to photo.dataset
. from console logs looks like I am getting my initial src
but then galModalImg.toString().replace('src="#"', 'src=' + data.src);
this line is not working. Also I have tried galModalImg.getAttribute.toString().replace('src="#"', 'src=' + data.src);
Second issiue that i have it is taht I am getting src
only from first photo only :/
Any ideas hov to get this working?