1

When changing a color of a paragraph element, CSS hover stops working.

I made a demo to explain: https://jsfiddle.net/woan6b64/

After I change <p>'s color, the hover selector stops working.

My question is:

  • How can I change the hover effect with JavaScript?
  • How can I get hovering to work after a color change?

JSFiddle code:

var shift = 0;

function change() {
  if (shift === 0) {
    document.getElementById("box").style.backgroundColor = "black";
    document.getElementById("text").style.color = "white";
    document.getElementById("text").innerHTML = 'Good! Now click the box again.';
    shift = 1;
  } else {
    document.getElementById("box").style.backgroundColor = "white";
    document.getElementById("text").style.color = "black";
    document.getElementById("text").innerHTML = 'Hover effect is now broken :(';
  }
}
#box {
  height: 100px;
  width: 200px;
  background-color: white;
}

p:hover {
  color: green;
}
<div id='box' onclick='change()'>
  <p id='text'>
    Click me for this box to change color. (Notice how I turn green when hovered)
  </p>
</div>

1
  • yes, it is an option @Syden Commented Sep 27, 2017 at 0:56

5 Answers 5

4

Stop it! Don't use !important if not necessary... your problem is that you set the color to black.

document.getElementById("text").style.color = "";

This will make the color inherit the right style.

How ever, this ain't the right solution either. You should add class to the box and then do:

#box {
  height: 100px;
  width: 200px;
  background-color: white;
}

p:hover {
  color: green;
}

#box.altered {
  color: white;
  background-color: black;
}

.altered p:hover {
  color: red;
}
<div id="box" onclick="this.classList.toggle('altered')">
  <p id='text'>
    Click me for this box to change color. (Notice how I turn green when hovered)
  </p>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Sure, but it also depends whether they want green on hover after being clicked the first time. That wasn't specified in the original question.
True, still adding class is much better option, and adding important to the green will make it work only with green... so
Yeah, a class is definitely the best option! All depends on the original goal, really.
1

Add a new class

// document.getElementById("box").style.backgroundColor = "black";
// document.getElementById("text").style.color = "white";
document.getElementById("text").classList.add("purple");

.purple {
  color: purple;
}

1 Comment

The way you did it would've worked, but this way is much cleaner and prevents adding it a second time.
0

If you want to do it with pure JS you need to listen for onmouseover event and onmouseout event, check the code that I made

var textElement = document.getElementById("text");
var defaultColor = textElement.style.color;

textElement.onmouseover = function(event) {
var currentElement = event.target;
currentElement.style.color = "red";
}

textElement.onmouseout = function(event) {
var currentElement = event.target;
currentElement.style.color = defaultColor;
}
<div id="parent">
<p id="text">
I am a text that change its text color :D
</p>
</div>

Comments

0

It's an issue with specificity, so you'll need to use !important on the color property to force it.

fiddle

NB: This isn't best practice. Depending on your intent, adding a class or simply unsetting the color may be the best option.

Comments

-1

After setting the color, the hover is overridden. Use !important to force it:

p:hover{
  color: green !important;
}

2 Comments

This ain't right! Don't use important if not strictly necessary this is bad practice...
While this is the solution, !important should be used as a last resort. You should make use of higher specificity instead :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.