Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda