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>