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

109
Visualizações
Change css float properity on click with javascript

I'm trying to do a dark mode button for my website. my idea is to use an event on click to change the side of my button switch from left to the right and right to the left. it works on my first click, it switches from left to the right (from off to on). But when I try to switch it back again nothing happens. Here's my code

const buttonmode = document.querySelector("#darkmode");
const switchmode = document.querySelector("#darkswitch");

buttonmode.addEventListener("click",(e) =>{
    e.preventDefault();
    if(switchmode.style.float="left"){

        switchmode.style.float="right";
    } 
    else if(switchmode.style.float="right"){
        
        switchmode.style.float="left";
    }
});
heres my css
button span{
    display: block;
    background: #999;
    height: 26px;
    width: 26px;
    border-radius: 50%;
    margin-left: 2px;
    float: left;
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In your conditional statement, in order to check what the current float value is you need to use == not just =

const buttonmode = document.querySelector("#darkmode");
const switchmode = document.querySelector("#darkswitch");

buttonmode.addEventListener("click",(e) =>{
    e.preventDefault();
    if(switchmode.style.float == "left") {
        switchmode.style.float="right";
    } else {
        switchmode.style.float="left";
    }
});
#darkswitch {
  width:50px;
  height:50px;
  background:black;
}
<button id="darkmode">Dark Mode</button>
<div id="darkswitch"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I would suggest using classList.toggle

const themeSwitcher = document.getElementById("themeSwitcher");

themeSwitcher.addEventListener("click", function () {
  this.classList.toggle("dark");
});
#themeSwitcher {
  width: 70px;
  height: 30px;
  border-radius: 50px;
  background-color: #ffffff;
  border: 2px solid #252525;
}

#toggler {
  float: left;
  width: 30px;
  height: 30px;
  background-color: #000000;
  border-radius: 50%;
}

.dark #toggler {
  float: right;
}
<div id="themeSwitcher">
  <div id="toggler"></div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your problem is you define switchmode variable outside of the function and re-use it in your function. Whenever DOM gets updated, your variable is not updated as you expected. You should get switchmode again for the next updates in the function.

You also have a small problem here

if(switchmode.style.float="left")

It should be === instead of = for if-else condition.

const buttonmode = document.querySelector("#darkmode");

buttonmode.addEventListener("click",(e) =>{
    e.preventDefault();
    const switchmode = document.querySelector("#darkswitch"); //move it here
    if(switchmode.style.float==="left"){

        switchmode.style.float="right";
    } 
    else if(switchmode.style.float==="right"){
        
        switchmode.style.float="left";
    }
});
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