Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Why doesn't my script find the element with this id?

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda