Using JS & HTML markup Always works: Codepen
if (document.forms['myForm'].name.value == '') {
name.style.background = "pink";
}
else {
name.style.background = "white";
}
Using JS & CSS Doesn't always work: Codepen
if (document.forms['myForm'].name.value == '') {
// give this element the .error class from the css file
name.className = name.className + " error";
}
else {
// this removes the error class
name.className = name.className.replace(" error", "");
}
Why is JS & CSS inconsistent? I would like to use this approach since I can just apply an error class to each of the elements I want to effect.