Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!