• 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 / Using jQuery to get Textarea Value

Using jQuery to get Textarea Value

February 25, 2022 Leave a Comment

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:

<form>
  <label for="desc">Description:</label>
  <textarea type="text" id="desc" name="desc"></textarea>
  <button type="submit" value="Submit">Submit</button>
</form>

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:

<form id="form-1">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" value="theprogrammingexpert" disabled>
  <label for="pass">Password:</label>
  <input type="password" id="pass" name="password" value="password" disabled>
  <label for="desc">Bio:</label>
  <textarea type="text" id="desc" name="desc" disabled>This is where text would go to describe your profile.</textarea>
</form>
<div id="div1">
  <label for="newbio">Change bio:</label>
  <textarea type="text" id="new-bio" name="newbio"></textarea>
  <div id="click-me">Update</div>
</div>

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:

<form id="form-1">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" value="theprogrammingexpert" disabled>
  <label for="pass">Password:</label>
  <input type="password" id="pass" name="password" value="password" disabled>
  <label for="desc">Bio:</label>
  <textarea type="text" id="desc" name="desc" disabled>This is where text would go to describe your profile.</textarea>
</form>
<div id="div1">
  <label for="newbio">Change bio:</label>
  <textarea type="text" id="new-bio" name="newbio"></textarea>
  <div id="click-me">Update</div>
</div>

<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.

Other Articles You'll Also Like:

  • 1.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 2.  How to Use jQuery to Remove Element
  • 3.  Changing the Background Image of a div Using jQuery
  • 4.  jQuery rotate – How to Rotate an Image using jQuery
  • 5.  Using jQuery to Get Value of Select On Change
  • 6.  Using jQuery to Change Text of HTML Element
  • 7.  Using jQuery to Change the Id of a Div
  • 8.  jQuery get img src – Get the Source of an Image Using jQuery
  • 9.  Using jQuery to Capitalize the First Letter of a String
  • 10.  jQuery text – Set the text of an HTML 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

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

x