Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!