01 / 03
Validate the input
INPUT · Slides
Show an error message
02 / 03
Use hidden to show and hide
HTML has a built-in way to keep an element invisible — the hidden attribute. An element with hidden does not appear on screen.
Put <p id="error" hidden></p> in the HTML. When there is an error, set error.hidden = false to reveal it. When the input is fine, set it back to true.
// show the errorerror.textContent = "Type something first"error.hidden = false// hide the errorerror.hidden = true03 / 03
Clear the error on good input
After you show the error, if the user types something valid and submits, the error should go away.
So when the guard passes (there is real text), hide the error. Always think about showing and hiding together.