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

184
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'setItem')

I want to create a simple light/dark mode system with javascript. It works by setting the 'darkTheme' boolean in Window.localStorage Here is the code I wrote:

let lightThemeCheckbox = document.getElementById('lightThemeCheckbox');
let body = document.body;

updateLightTheme();

lightThemeCheckbox.addEventListener('change', function() {
    Window.localStorage.setItem('darkTheme', this.checked);
});

function updateLightTheme() {
    let darkTheme = window.localStorage.getItem('darkTheme');

    if(darkTheme == undefined) {
        body.style.backgroundColor = 'rgb(255, 255, 255)';
        return;
    }

    body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)';
}

But for some reason, I get this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'setItem')
at HTMLInputElement.<anonymous> (header.js:7)
(anonymous) @ header.js:7

Can anyone help?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have put "window" with a capital w. You can use the following:

Using lowercase window

const lightThemeCheckbox = document.getElementById('lightThemeCheckbox');
const body = document.body;

updateLightTheme();

lightThemeCheckbox.addEventListener('change', () => {
    window.localStorage.setItem('darkTheme', this.checked);
});

function updateLightTheme() {
    const darkTheme = window.localStorage.getItem('darkTheme');

    if (darkTheme == undefined) {
        body.style.backgroundColor = 'rgb(255, 255, 255)';
        return;
    }

    body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)';
}

Using localStorage without window

const lightThemeCheckbox = document.getElementById('lightThemeCheckbox');
const body = document.body;

updateLightTheme();

lightThemeCheckbox.addEventListener('change', () => {
    localStorage.setItem('darkTheme', this.checked);
});

function updateLightTheme() {
    const darkTheme = localStorage.getItem('darkTheme');

    if (darkTheme == undefined) {
        body.style.backgroundColor = 'rgb(255, 255, 255)';
        return;
    }

    body.style.backgroundColor = darkTheme ? 'rgb(100, 100, 100)' : 'rgb(255, 255, 255)';
}

Hoped this helped!

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