0

I have this img element in my html file:

<div id="runningQuizBoxForFlag">
    <div class="flagBoxQuiz">
        <img id="a" src="https://flagcdn.com/h120/im.png" srcset="https://flagcdn.com/h240/im.png 2x" alt="Isle of Man">
    </div>
</div>

and when I'm trying to access it from my typescript file, it doesn't find the element and returns null.

const img: HTMLImageElement = document.getElementById('a') as HTMLImageElement;
console.log("img is " + img);
    
img.src = "https://flagcdn.com/h120/" + country.code + ".png"; //error occurs here
img.srcset = "https://flagcdn.com/h240/" + country.code + ".png";
img.alt = country.alt;

with this error: Uncaught TypeError: Cannot set properties of null (setting 'src')

I have changed the id to "a" so i don't make spelling mistakes, but it still doesn't work.

0 Answers0