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

120
Visualizações
Activate hover color after toggle function

I have read a lot of posts here but I can't find the solution.. Any ideas why hover color doesn't work on second click? (example with snippet or jsfiddle please) Thanks!!!

function toggleButton() {
  var button = document.getElementById('toggle').style.backgroundColor;
  var color = '';

if (button !== 'pink') {
  color = 'pink';
document.getElementById('toggle').style.backgroundColor = color;
document.getElementById('toggle').style.color = "#222";

} else if (button === 'pink') {
  color = '#2c475c';
document.getElementById('toggle').style.backgroundColor = color;
document.getElementById('toggle').style.color = "#fff";
  }
}
#toggle {    
  padding: 20px 45px;
  background: green;
  color: #fff;
  border-radius:5px;
  font-weight:600; 
}

#toggle:hover {  
  background: aqua;
}
<button  id="toggle" onclick="toggleButton();">TOGGLE</button >

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

0

from comment

when you set style via javascript, it is included into a style attribute, which overwrites any other style you set from a style sheet. Instead setting new style, add a class classList.add('myNewClass') , so it can be parts of the style sheet with rules that are not the most important ones

example

document.getElementById("toggle").addEventListener("click", myFunction);

function myFunction() {
  if (this.classList.contains('pink')) {
      this.classList.toggle('darkblue');
    }
    this.classList.toggle('pink');
  };
#toggle {
  padding: 20px 45px;
  background: green;
  color: #fff;
  border-radius: 5px;
  font-weight: 600;
}

#toggle:hover,
#toggle.pink:hover,
#toggle.darkblue:hover{
  background: aqua;
}

#toggle.pink {
  background-color: pink;
  color: #222;
}

#toggle.darkblue {
  background: #2c475c;
  color: white;
}
<button id="toggle">TOGGLE</button >

about 4 years ago · Juan Pablo Isaza Relatório

0

Because you set the background with inline style, which has greater specificity than your CSS. If you want to override any inline style, you have to make your style in CSS !important.

#toggle:hover {  
  background: aqua !important;
}

Edit

As mentioned by @G-Cyrillus, the better way is to create classes with the color you need for the button and toggle those classes (and not inline style). For you code, it would be good to create 2 classes: for pink and grey.

.pink {
  color: #222;
  background-color: pink;
}
.grey {
  color: #fff;
  background-color: #2c475c;
}

Now you just need to edit your JS file, so it works with your new classes.

function toggleButton() {
  var button = document.getElementById("toggle");

  if (button.classList.contains("grey")) {
    button.classList.remove("grey");
    button.classList.add("pink");
  } else {
    button.classList.remove("pink");
    button.classList.add("grey");
  }
}

And because :hover has greater specificity than plain pink or grey class, it will trigger your hover styles.

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