To get the value from a textarea field, we can use the jQuery val method. We can also use the val() method to set the value of a textarea field.

var userInput = $("textarea").val();

Let’s say we have the following HTML:

To get the value that the user has entered for “Description”, we would use the jQuery val() method.

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

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

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

This can also be done using plain JavaScript with the value property.

Using jQuery 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 jQuery code below, we will get the new user textarea value using the jQuery val() method, and then set the textarea value “Bio” in the form above using the val() method as well.

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

$("#click-me").click(function(){
  var userInput = $("#new-bio").val();
  $("#desc").val(userInput);
});

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

Code Output:








Update

Full Code:

Update
<script> $("#click-me").click(function(){ var userInput = $("#new-bio").val(); $("#desc").val(userInput); }); </script>

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

Categorized in:

jQuery,

Last Update: March 14, 2024