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

145
Visualizações
changing a document's theme using multiple index files

I have a total of like 5 index.html files. They all have different index names and are linked to one JS file. When I click the button to change the theme of the index.html page. The theme changes to light theme and set in localStorage. The other pages are not changed. Can someone get me in the right direction. What am I doing wrong?

// Switch theme/add to local storage to all body elements in multiple .html files
const bodyElement = document.body
// Get the theme button via id
const themeToggleBtn = selectElement('#theme-toggle-btn');
//  passes the theme button to the toggleTheme function
const currentTheme = localStorage.getItem('currentTheme');

// checks if there is a current theme in local storage
if (currentTheme) {
    bodyElement.classList.add('light-theme');
}

//  event listener  btn is clicked the body element changes theme
themeToggleBtn.addEventListener('click', () => {
    bodyElement.classList.toggle('light-theme');

    //  checks if element has the class and sets it in local storage 
    if (bodyElement.classList.contains('light-theme')) {
        localStorage.setItem('currentTheme', 'themeActive');
        //  if not it removes it
    } else {
        localStorage.removeItem('currentTheme');
    }
});
    <script src="./assets/js/main.js"></script>

about 4 years ago · Juan Pablo Isaza
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