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

168
Visualizações
Issue with access a variables value outside of a function
let dark = true;
function toggle() {
    dark = false;
    document.body.classList.toggle('dark-mode');
}

if (dark = false) {
    document.getElementById("logo").src="../../img/Kevin D (White).png";
}else {
    document.getElementById("logo").src="../../img/Kevin D (Black).png";
}

Above is my code. Whenever I click the button the image does not go to the white version, instead it just stays black even on a black background, how would I fix this? The code is also attached to a button which would call that function

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

0

There are a few things to solve here:

  • You need to call the if again when the variable changes, we do this by wrapping it in a function, then calling it on load and whenever the toggle is called.
  • Toggle currently doesn't "toggle", set the value to !dark to invert it every time.
  • The if is currently assigning the value false to the dark variable, we need to compare it instead, and since it's already a boolean, it can simply be if (dark) {}

var dark = true;

function toggle() {
  dark = !dark; // To actually toggle the variable you need to invert the current value
  document.body.classList.toggle('dark-mode');
  // After the variable is toggle, call the function to update the theme
  changeTheme()
}

function changeTheme() {
  if (dark) {
    console.log("It's dark!")
  } else {
    console.log("It's light!")
  }
}

// Call the function on load to set the initial theme
changeTheme()

// Add an event listener to call the toggle function
document.getElementById("toggle").addEventListener("click", toggle);
<div id="toggle">Toggle</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Your function is called 'toggle' but at the moment would only set dark to false. So I would change that to dark = !dark, so it's always setting to the opposite.
  • Next step is to include the if-else block inside the function, so it is executed when toggling.
  • The single equal sign in the if statement was already mentioned...
  • instead of if(dark === false) you can simply check if(!dark) or better swap the logic to not begin with a negation and say if(dark){'dark'}else{'not dark}
  • It looks like you have blanks inside the filenames of the images, which might be probably better avoided...

let dark = true;

function toggle() {

  dark = !dark;
  
  document.body.classList.toggle('dark-mode');

  if (dark === false) {
    console.log('dark ===', dark, '>> set white img')
    // document.getElementById("logo").src = "../../img/Kevin D (White).png";
  } else {
    console.log('dark ===', dark, '>>set black img')
    // document.getElementById("logo").src = "../../img/Kevin D (Black).png";
  }
}
.dark-mode {
  background: black;
  color: white;
}
<button onclick="toggle()">toggle</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You have an error with the = sign. It should be ==

if (dark == false) {
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