Categories
JavaScript Answers

How to clear text area on click with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.