I'm making a form for registration and want to show error in another element if the input doesn't match a condition.
//input element
let field = document.getElementById("field").value.trim();
//div for showing error
let field_error = document.getElementById("field_error").textContent;
if(field.length > 30){
field_error = "Input too long";
}
If I write code like this it doesn't do anything, but if I put like this:
let field_error = document.getElementById("field_error");
if(field.length > 30){
field_error.textContent = "Input too long";
}
Why can't I just declare a variable field_error with .textContent, as I did with field and .value.trim(), instead using .textContent later?