• 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
  • Ruby
  • 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.  Using jQuery prepend to Insert Element As First Child
  • 2.  jQuery keyup Method
  • 3.  Using jQuery to Add a Sibling to an Element
  • 4.  Using jQuery to Select Multiple Ids
  • 5.  jQuery after – Insert HTML After Another Element
  • 6.  Using jQuery to Get the Bottom Position of Element
  • 7.  jQuery Opacity – How to Change the Opacity of an Element
  • 8.  How to Use jQuery to Check if an Element is Visible
  • 9.  jQuery prev – Get the Previous Element of a Div
  • 10.  Examples of Text Animations Using jQuery

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy