How to change the image src of changing Heroimages

Hello :slight_smile:
For my studies I have to create content to my site which changes daily. I decided to use Heroimages but no matter what I try I always get stuck. I hope you can help me. I don’t want to use document.write but something seems to be wrong in one of the bottom lines. The error is: TypeError: Cannot read property ‘appendChild’ of null

This is my script:

var imlocation = "../public_html/media/img/";

 function ImageArray (n) {

   this.length = n;

   for (var i =1; i <= n; i++) {

     this[i] = ' '



image = new ImageArray(7);

image[0] = 'HeroImage1.jpg';
image[1] = 'HeroImage2.jpg';

image[2] = 'HeroImage1.jpg';

image[3] = 'HeroImage2.jpg'; 

image[4] = 'HeroImage1.jpg';

image[5] = 'HeroImage2.jpg';

image[6] = 'HeroImage1.jpg';

var currentdate = new Date();

var imagenumber = currentdate.getDay(); 

var position = document.getElementById('position').appendChild(img);

img.src = imlocation + image[imagenumber];

Thank you a lot.

Hi there and welcome to the forums! The error states that the appendChild() method is trying to act on an element that is null. The only place in that code this could be is here

therefore this means that document.getElementById('position') is returning null, which usually means that there is no element with the ID position. I would check your HTML to make sure that an element with that ID is there, and if you are still stuck please also post your HTML code to assist!

Thanks for the fast answer! :slight_smile:
The Id belongs to a span element. I’ll link it in a picture.
I tried linking the script on the bottom of the html instead of the head an now it return: ReferenceError: img is not defined