Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Color change HTML

I would like an ‘input type="text"’, if the values are greater than 50 then the text should appear in green, is the value between 25 and 50 orange and less than 25 red.

But I don’t know how to make it work exactly can someone help me with such an HTML file?

<body>
<form>
  <label for="fname">Value</label>
  <input type="text" id="value" name="value">
</form>
if value is less than 25 > red,
if value is between 25 - 50 > orange, 
if value is greater than 50 > green.
</body>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Although you should show your attempts, I assume you cannot figure it out on your own. Below is a simple demonstration, if you enter a number in the input field which is below 50 it appears green, above and it appears red.

It would be better if you can use this example to implement your understanding now.

const input = document.getElementById('number')

input.onkeyup = function () {
  if (Number(input.value) < 25) input.style.color = "red"
  if (Number(input.value) > 25 && Number(input.value) < 50) input.style.color = "orange"
  if (Number(input.value) > 50) input.style.color = "green"

}
<input type="text" id="number">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading