When conducting testing, the color may appear unknown since it is not defined inline. To confirm, you can utilize computed styles or opt for a ternary operator
function myFunction() {
const lol = document.getElementById("lol");
console.log(window.getComputedStyle(lol, null).color)
// Rather than complex logic, consider switching to black if it's not already black
lol.style.color = lol.style.color === "black" ? "white" : "black";
}
p {
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol">adadada</p></code></pre>
</div>
</div>
</p>
<p>An alternative and more efficient solution would be toggling the class</p>
<p><div>
<div>
<pre class="lang-js"><code>function myFunction() {
const lol = document.getElementById("lol");
console.log(lol.classList.contains("white")); // can be used or simply toggle
lol.classList.toggle("white")
}
.white {
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol" class="white">adadada</p></code></pre>
</div>
</div>
</p>
</div></answer2>
<exanswer2><div class="answer" i="69392583" l="4.0" c="1632995602" m="1633007624" v="-1" a="bXBsdW5namFu" ai="295783">
<p>The exact color is uncertain during testing due to it not being directly specified. Validate by checking the computed style or employ a simple ternary operation</p>
<p><div>
<div>
<pre class="lang-js"><code>function myFunction() {
const lol = document.getElementById("lol");
console.log(window.getComputedStyle(lol, null).color)
// Simplify by changing to black when not already black
lol.style.color = lol.style.color === "black" ? "white" : "black";
}
p {
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol">adadada</p>
Optimal Method: Toggle the class
function myFunction() {
const lol = document.getElementById("lol");
console.log(lol.classList.contains("white")); // potential usage or direct toggle
lol.classList.toggle("white")
}
.white {
color: white;
}
<button onclick="myFunction()">Try it</button>
<p id="lol" class="white">adadada</p>