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

320
Vistas
Why does not JS background colour of input field check work?

I want to check if the background colour of an input field is e.g. green when the input is changed. I have tried most things, but nothing seems to work. Why does this not work? I think it is the condition in the if statement that does not work, as I have tried both alert and value inside the if statement.
The following is a minimal reproducible example:

function myFunction(chosenID) {
  if (document.getElementById(chosenID).style.backgroundColor == "#99ff99") {
    document.getElementById(chosenID).value = "You tried to change a form with a background colour already"
  }
}
<html>

<body>
  <input type="text" id="a" value="Non-coloured input field" onChange="myFunction('a')">
  <input type="text" id="b" value="Coloured input field" onChange="myFunction('b')" style="background-color: #99ff99">
</body>

</html>


Edit

I do not think that this is a duplicate. My question is about not getting the desired result from a function, not about conversion between RGB and HEX. Therefore, this should be reopened.
about 4 years ago · Juan Pablo Isaza
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