• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / jQuery / How to Use jQuery to Clear a textarea Field

How to Use jQuery to Clear a textarea Field

April 30, 2022 Leave a Comment

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:

<div id="div1">
  <textarea type="text" id="userText">This is some default text.</textarea>
</div>

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:

<div id="div1">
  <textarea type="text" id="userText">This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.  Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate.  At the end of the day, we want to be able to just push a button and let the code do its magic.  Below, you can select a category and browse posts in a category.  Thank you for reading!</textarea>
  <div id="click-me">Clear text above</div><br>
</div>

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:

<div id="div1">
  <textarea type="text" id="userText">This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.  Working with Python, Javascript, PHP, HTML, SAS, and other programming languages can allow us to create amazing programs which make our work more efficient, repeatable, and accurate.  At the end of the day, we want to be able to just push a button and let the code do its magic.  Below, you can select a category and browse posts in a category.  Thank you for reading!</textarea>
  <div id="click-me">Clear text above</div>
</div>

<script>

$("#click-me").click(function(){
  $("#userText").val("");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery width() – Using jQuery to Get the Width of a Div
  • 2.  jQuery delay – How to Delay the Start of a Method
  • 3.  Using jQuery to Count Children of an Element
  • 4.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 5.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 6.  jQuery after – Insert HTML After Another Element
  • 7.  Using jQuery to Get the Position of Element
  • 8.  jQuery before – Insert HTML Before Another Element
  • 9.  How to Check if Element Exists Using jQuery
  • 10.  jQuery insertAfter – Insert Element After Another Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy