I have a problem when I try to add a class in the catch statement.
When I click the button to confirm, javascript show me the error with the class successfully added, but when I reclick the button, without refreshing the page, the class added is no longer present.
function catchclass(){
var result, x;
result = document.getElementById("result");
result.innerHTML = "";
x = document.getElementsByTagName("input")[0].value;
try {
if(x == "") throw "is Empty";
}
catch(err) {
result.innerHTML = "Input " + err;
result.className += 'error';
}
}
p.error {
background: red;
padding: 10px;
}
<input type="text" />
<button type="button" onclick="catchclass()">Test Input</button>
<p id="result"></p>