Issue
Why is my code not working?
I want to check if the color of the paragraph is white(it is white because of the css) and when it is white i want to change it to black so it is visible
function myFunction() {
if (document.getElementById("lol").style.color === "white") {
document.getElementById("lol").style.color = "black";
}
}
p {
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol">adadada</p>
Solution
The element does not have that property because it is defined in the css class. you need to define it inline or with javascript.
function myFunction() {
if (document.getElementById("lol").style.color === "white") {
document.getElementById("lol").style.color = "black";
}
}
p { /* this is useless now */
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol" style="color: white;">adadada</p>
Answered By - Jesper
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.