Sometimes, we want to check if a text area is empty in JavaScript.
In this article, we’ll look at how to check if a text area is empty in JavaScript.
How to check if a text area is empty in JavaScript?
To check if a text area is empty in JavaScript, we can check if e.target.value
is falsy or not.
For instance, we write:
<textarea></textarea>
to add a text area.
Then we write:
const textarea = document.querySelector('textarea')
textarea.onchange = (e) => {
if (!e.target.value) {
console.log('empty')
}
}
to select the text area with querySelector
.
And we set textarea.onchange
to a function that checks if e.target.value
is falsy to check if it’s empty when we type into the text area.
Conclusion
To check if a text area is empty in JavaScript, we can check if e.target.value
is falsy or not.