Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
Javascript check if color is white not working

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>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't know the color at the time you test because the color is not inline. You need to check the computed style or just use a ternary

function myFunction() {
  const lol = document.getElementById("lol");
  
  console.log(window.getComputedStyle(lol, null).color)

// the above is too complicated so why not just change to black if not black?
  
  lol.style.color = lol.style.color === "black" ? "white" : "black";
}
p {
  color: white;
}
<button onclick="myFunction()">Try it</button>

<p id="lol">adadada</p>

Better: Toggle the class

function myFunction() {
  const lol = document.getElementById("lol");
  
  console.log(lol.classList.contains("white")); // can be used or just toggle

  lol.classList.toggle("white")
}
.white {
  color: white;
}
<button onclick="myFunction()">Try it</button>

<p id="lol" class="white">adadada</p>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda