We can use jQuery to clear a textarea field easily by making use of the jQuery val() method. We simply need to set the value of the textarea to an empty string.

$(textarea).val("");

Let’s see a quick example of this below:


Let’s say we have the following HTML:

The textarea in the example above will have the text “This is some default text.”. If we want to clear the textarea in the example above, we would use the following jQuery code:

$("#userText").val("");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#userText").val("");

We can also clear a textarea field using just plain JavaScript by targeting the value property.

Let’s do an interactive example of clearing a textarea below.

Using jQuery to Set and Clear a textarea Field with a Click

Below we will have a simple textarea input. It will start prefilled with some text from our about page. We will then let the user clear the textarea.

Here is our simple HTML:

Clear text above

We will simply use the jQuery val() method to set the textarea field to an empty string.

Here is the JavaScript and jQuery code:

$("#click-me").click(function(){
  $("#userText").val("");
  //That's it!
});

The final code and output for this example of using jQuery to clear a textarea field is below:

Code Output:

Clear text above

Full Code:

Clear text above
 

Hopefully this article has been useful to help you understand how to use jQuery to clear a textarea.

Categorized in:

jQuery,

Last Update: April 1, 2024