Sometimes, we want to clear a text area on focus with JavaScript.
In this article, we’ll look at how to clear a text area on focus with JavaScript.
Clear a Text Area on Focus with JavaScript
To clear a text area on focus with JavaScript, we can call the jQuery focus
method with a callback that sets the value of the text area to an empty string.
For instance, we write:
<textarea></textarea>
to add a text area.
Then we write:
$('textarea').focus(function() {
$(this).val('');
});
We select the text area with $('textarea')
.
Then we call focus
with a callback that sets the text area’s value to an empty string by writing:
$(this).val('');
Conclusion
To clear a text area on focus with JavaScript, we can call the jQuery focus
method with a callback that sets the value of the text area to an empty string.