/    Sign up×
Community /Pin to ProfileBookmark

Can’t access images added to HTML, shows an empy nodeList

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).

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJan 22.2021 — 
can't really say without seeing your code but

generally I would say

innerHTML should always be thought of as "READ ONLY"

use document.createElement('img')

then set img src to src of image
Copy linkTweet thisAlerts:
@_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
Copy linkTweet thisAlerts:
@SempervivumJan 22.2021 — @_Yar_#1627041 Please don't post code as an image. Copy and paste the text and use code tags: `your code here`
Copy linkTweet thisAlerts:
@VITSUSAJan 22.2021 — @_Yar_#1627041 I agree with Sempervivum, please share your code, not an image of the code.
Copy linkTweet thisAlerts:
@_Yar_authorJan 22.2021 — @Sempervivum#1627044 okay, thank you.

<i>
</i>let slidesContainer;
let switchBtns;
let popupText;
let slideImages;

slidesContainer = document.querySelector('.img-slides');
popupText = document.querySelector('.pop-up-text');
slideImages = slidesContainer.querySelectorAll('.slide-box_img');
switchBtns = document.querySelectorAll('.switch-btn');

// * Loading default items

let slides = [
[
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide1.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide2.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide3.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>
],
[
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide4.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide5.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide6.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>
],
[
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide7.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide8.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>,
<span><code>&amp;lt;img class=&quot;slide-box_img&quot; src=&quot;./img/slide9.jpg&quot; alt=&quot;slide&quot;&amp;gt;</code></span>
]
];


let loadOne, loadTwo, loadThree;

loadOne = slides[0].join('');
loadTwo = slides[1].join('');
loadThree = slides[2].join('');


// * Loading the first load of images

window.addEventListener('DOMContentLoaded', ()=&gt;{
slidesContainer.innerHTML = loadOne;
document.querySelector('.left').style.backgroundColor = 'rgb(4, 134, 128)';
});

console.log(slideImages); // ------ shows an empty nodeList



// * Switch Slides-Images

switchBtns.forEach((btn)=&gt;{
btn.addEventListener('click', (e)=&gt;{
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';
}

<i> </i>if(target.classList.contains('center')){
<i> </i> slidesContainer.innerHTML = loadTwo;
<i> </i> document.querySelector('.center').style.backgroundColor = 'rgb(4, 134, 128)';
<i> </i> document.querySelector('.left').style.backgroundColor = 'white';
<i> </i> document.querySelector('.right').style.backgroundColor = 'white';
<i> </i>}

<i> </i>if(target.classList.contains('right')){
<i> </i> slidesContainer.innerHTML = loadThree;
<i> </i> document.querySelector('.right').style.backgroundColor = 'rgb(4, 134, 128)';
<i> </i> document.querySelector('.center').style.backgroundColor = 'white';
<i> </i> document.querySelector('.left').style.backgroundColor = 'white';
<i> </i>}
})
})
Copy linkTweet thisAlerts:
@SempervivumJan 22.2021 — Single backtics don't work reliably when posting code. You should use code tags: `your code here` or triple backtics.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@SempervivumJan 22.2021 — There are two reasons for this issue:
  • 1. Lets focus on this piece of code:
    // * Loading the first load of images

    window.addEventListener('DOMContentLoaded', ()=&gt;{
    slidesContainer.innerHTML = loadOne;
    document.querySelector('.left').style.backgroundColor = 'rgb(4, 134, 128)';
    });

    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.

    https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

    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:
  • 1. `let slidesContainer = document.querySelector('.img-slides');`

    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)=&gt;{
    document.addEventListener('click', (e)=&gt;{
    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';
    }

    if(target.classList.contains('center')){
    slidesContainer.innerHTML = loadTwo;
    document.querySelector('.center').style.backgroundColor = 'rgb(4, 134, 128)';
    document.querySelector('.left').style.backgroundColor = 'white';
    document.querySelector('.right').style.backgroundColor = 'white';
    }

    if(target.classList.contains('right')){
    slidesContainer.innerHTML = loadThree;
    document.querySelector('.right').style.backgroundColor = 'rgb(4, 134, 128)';
    document.querySelector('.center').style.backgroundColor = 'white';
    document.querySelector('.left').style.backgroundColor = 'white';
    }
    })
    //})
  • Copy linkTweet thisAlerts:
    @_Yar_authorJan 22.2021 — @Sempervivum#1627088 oh... I see, thanks!
    Copy linkTweet thisAlerts:
    @_Yar_authorJan 22.2021 — @Sempervivum#1627090 thank you for the explanation!
    Copy linkTweet thisAlerts:
    @SempervivumJan 22.2021 — You're welcome, have a nice weekend!
    Copy linkTweet thisAlerts:
    @JennieMillerJan 26.2021 — thank you so much
    ×

    Success!

    Help @_Yar_ spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...