Hi everyone! Please help me to figure this one out. I’m trying to add images with **innerHTML**, but after I load the set of images using **DOMContentLoaded**, I can’t access them, because the console shows an empty **nodeList**. Thanks in advance!! Here’s the [code](https://drive.google.com/file/d/1BiZSk8IZW9tPsUvdiijBMLw07eOWycmQ/view?usp=sharing).
@_Yar_authorJan 22.2021 — #@DaveyErwin#1627029 thank you very much, I'll try that way. Here's my code - https://drive.google.com/file/d/1BiZSk8IZW9tPsUvdiijBMLw07eOWycmQ/view?usp=sharing
console.log(slideImages); // ------ shows an empty nodeList You are presetting the innerHTML of slidesContainer inside the eventlistener for DOMContentLoaded. It will take a bit of time until this event fires. If you log the variable slideImages right after adding the event listener it is not yet preset.
2. MDN says:
>The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
Your code presets the variable slideImages at the beginning when its innerHTML is not yet set. As this node list is not live it will remain empty even if you set the innerHTML later.
BTW: Your code can be simplified and shortened a bit:
instead of let slidesContainer; slidesContainer = document.querySelector('.img-slides');
2. You can take advantage of event bubbling and make the loop for adding the eventlisteners obsolete: // switchBtns.forEach((btn)=>{ document.addEventListener('click', (e)=>{ let target = e.currentTarget; if(target.classList.contains('left')){ slidesContainer.innerHTML = loadOne; document.querySelector('.left').style.backgroundColor = 'rgb(4, 134, 128)'; document.querySelector('.center').style.backgroundColor = 'white'; document.querySelector('.right').style.backgroundColor = 'white'; }