To clear text area on click with JavaScript, we set the text area’s value
property to an empty string.
For instance, we write
<textarea id="text-area" value="Your text inside the textarea"> </textarea>
<button onClick="clear();">Your button Name</button>
to add a text area and a button.
We set onclick
to clear();
to call the clear
function when we click it.
Then we write
function clear() {
document.getElementById("text-area").value = "";
}
to define the clear
function that selects the text area with getElementById
.
And we clear its value by setting its value
property to an empty string.
Conclusion
To clear text area on click with JavaScript, we set the text area’s value
property to an empty string.