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

191
Vistas
I want to export an object from index.js to exam.js

Both the scripts files index.js and exam.js are linked respectively to index.html and exam.html. but styling in index.js is causing this error

(index.js:193 Uncaught TypeError: Cannot read properties of null (reading 'style') at index.js:193:37 (anonymous) @ index.js:193)*

in the exam.js file

index.js file setup

document.querySelector('.container').style.backgroundColor = 'green'
export const colors = {red: 'red', blue: 'blue'}

exam.js file setup

import {colors} from './index.js'
console.log(colors);

note both script tags have the type attribute (modules) in their respective html file. How do I stop the document.querySelector('.container').style.backgroundColor = 'green' from giving me the above error, because without the above styling the code exports perfectly?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Are you sure that '.container' exists in the DOM when the script is executed? Just try something like this:

export const colors = ...your object...;

const targetEl = document.querySelector('.container');

if(!targetEl) {
  return;
}
targetEl.style.backgroundColor = 'green';
about 4 years ago · Juan Pablo Isaza Denunciar
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