I am practicing JavaScript with a program that should change the image of a dice according to a random number. If I put this line in the console the images changes; it works.
document.querySelector("img.img1").setAttribute("src", "images/dice1.png");
but if I put it in the editor I get the following error message:
index.js:5 Uncaught TypeError: Cannot read properties of null (reading 'setAttribute') at index.js:5
Why?
I forgot differ the link of the js link file in the head. I put this line
<script src="index.js" charset="utf-8"></script>
only in the head of the html file. I should differ it or put it in the end of the body section.