• 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 / JavaScript / How to Clear a Textarea Field In JavaScript

How to Clear a Textarea Field In JavaScript

July 15, 2022 Leave a Comment

We can clear a textarea field using JavaScript easily by making use of the value property. We simply need to set the value of the textarea field to an empty string.

document.getElementById("textarea1").value = "";

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 field 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 JavaScript code:

document.getElementById("userText").value = ""

We can also clear a textarea field easily using jQuery by making use of the jQuery val() method.

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

Using JavaScript to Clear a Textarea Field with a Click

Below we will have a simple textarea field. 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" onclick="clearTextarea()">Clear textarea field above</div>
</div>

We will simply use the value property to set the textarea field to an empty string. We will need to put this code in a function that we call when the user clicks the button below the textarea field.

We will add an onclick event to our button below the textarea field that will call our function.

Here is the JavaScript function:

function clearTextarea(){
  document.getElementById("userText").value = "";
};

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

Code Output:

Clear textarea field 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" onclick="clearTextarea()">Clear textarea field above</div>
</div>

<script>

function clearTextarea(){
  document.getElementById("userText").value = "";
};

</script>

Getting the Value From an Textarea Field Using JavaScript

To get the value from a textarea field, we can once again use the value property.

Let’s say we have the following HTML:

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

To get the value from the textarea field, we would use the value property.

var textareaText = document.getElementById("userText").value;

Hopefully this article has been useful to help you understand how to clear a textarea in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if String Contains Only Letters
  • 2.  Set Hidden Field Value In JavaScript
  • 3.  Using Javascript to Remove Class from Element
  • 4.  Using JavaScript to Remove Apostrophe From a String
  • 5.  Add Hours to a Date Using JavaScript
  • 6.  Using JavaScript to Get New Date Format in dd mm yy
  • 7.  Using JavaScript to Get the Length of Array
  • 8.  Using JavaScript to Check a Radio Button
  • 9.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 10.  Using JavaScript to Round to 4 Decimal Places

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