To get the value from a textarea field in JavaScript, we can use the value property. We can also use the value property to set the value of a textarea field.

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

Let’s say we have the following HTML:

To get the value that the user has entered for “Description”, we would use the value property.

document.getElementById("desc").value;

This can also be done using jQuery with the val() method.

Using JavaScript to Get and Set a Textarea Value

In this example, we will have a form that will display the information of a user. We will then let the user update one of the fields by using a textarea field and submit button below it.

Here is the HTML setup:

Update

In the JavaScript code below, we will get the new user textarea value using the value property, and then set the textarea value “Bio” in the form above using the value property again along with the getElementById method.

Here is the JavaScript code for getting and setting the textarea value:

function updateBio(){
  var userInput = document.getElementById("new-bio").value;
  document.getElementById("desc").value = userInput;
};

The final code and output for this example of using JavaScript to get and set the value in a textarea field is below:

Code Output:








Update

Full Code:

Update
<script> function updateBio(){ var userInput = document.getElementById("new-bio").value; document.getElementById("desc").value = userInput; }; </script>

Hopefully this article has been useful to help you understand how to use JavaScript to get the value from a textarea field.

Categorized in:

JavaScript,

Last Update: March 14, 2024