Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda