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

262
Visualizações
Enable hover color after toogle button

So, If I put the Mouse over the "CHANGE" button, the orange hover color works normally.

If I click the "CHANGE" button once ,the color turns red but hover color disappears (that's ok).

If I press it 2 times ,it returns to the first color(blue), but the hover color doesn't work.

How can the hover color disappear in the first click (as it does now) and reappear in the second click, as in the beginning? Thanks a lot!!!

var someFunction = {};

function changeColor() {
        var button = document.getElementById('mute1').style.backgroundColor;
        var color = '';
        this.active = !this.active;
        someFunction["one"] = this.active;
        someFunction["two"] = this.active;
        if (button !== 'red') {
            color = 'red';
            document.getElementById('mute1').style.backgroundColor = color;
            document.getElementById('mute1').style.color = "#fff";
        } else if (button === 'red') {
            color = '#2c475c';
            document.getElementById('mute1').style.backgroundColor = color;
            document.getElementById('mute1').style.color = "#fff";
            this.active = !this.active;
            someFunction[this.function] = this.active;
        }
    }
#mute1 {    
  padding: 25px 50px;
    background: #2c475c;
    color: #fff;
  border-radius:5px;
}

#mute1:hover {  
  background: orange;
}
<button  id="mute1" onclick="changeColor();">CHANGE</button >
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that the hover only changes color via a class whereas the click changes the inline style and that takes precedence. If you look with your browser dev tools you'll see that the element has an inline background style set once you've done the first click.

If you want to stop that and always have a hover make the background color orange you have a couple of alternatives:

  • stick to changing the inline style on click but also add an event listener for mouseover in JS which changes inline background to orange or

  • use classes for everything.

This snippet removes the hover class and instead listens for mouseenter and mouseout events as well as the click on the button.

It makes the color variable global so that it can be also used in the mouseout event code to restore the color (the red or gray) that was there before.

The code here is rather basic, just within the button, you will probably want to make it into 'proper' JS functions and also to addEventListeners rather than use the inline onclick etc attributes.

let color = '#2c475c';
var someFunction = {};

function hovered() {}

function changeColor() {
  var button = document.getElementById('mute1').style.backgroundColor;
  /*var */
  color = '';
  this.active = !this.active;
  someFunction["one"] = this.active;
  someFunction["two"] = this.active;
  if (button !== 'red') {
    color = 'red';
    document.getElementById('mute1').style.backgroundColor = color;
    document.getElementById('mute1').style.color = "#fff";
  } else if (button === 'red') {
    color = '#2c475c';
    document.getElementById('mute1').style.backgroundColor = color;
    document.getElementById('mute1').style.color = "#fff";
    this.active = !this.active;
    someFunction[this.function] = this.active;
  }
}
#mute1 {
  padding: 25px 50px;
  background: #2c475c;
  color: #fff;
  border-radius: 5px;
}
<button id="mute1" onclick="changeColor();" onmouseenter="this.style.backgroundColor = 'orange';" onmouseout="this.style.backgroundColor = color;">CHANGE</button >

about 4 years ago · Juan Pablo Isaza Relatório

0

Remove the semi-colon in onclick:

<button  id="mute1" onclick="changeColor()">CHANGE</button >
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