I needed to create an error message in a distinctive color using CSS when a user enters a letter instead of a number in a form. I successfully implemented this feature, however, I encountered an issue where the error message remains visible even after the user corrects their input by entering a number. How can I resolve this problem?
<html>
<head>
<style>
#error {color: red;}
</style>
</head>
<body>
<p id="error"></p>
<p id="demo"></p>
<input id="demo1" type="text" />
<button onclick="vote()"> Click here</button>
<script>
function vote() {
var age = document.getElementById("demo1").value;
if (isNaN(age)) {
document.getElementById("error").innerHTML = "Input Error!!";
} else {
document.getElementById("demo").innerHTML = (age < 18) ? "Too young to vote" : "Old enough to vote";
}
}
</script>
</body>
</html>