JavaScript Set Textarea Value

In this tutorial we will learn how to Set Textarea Value with JavaScript. We can use JavaScript value property along with getElementById() method to set the value of textarea element.

value property

value property sets or returns the value of HTML input tags or form elements.

In this example we have used value property to set the value of textarea tag.

getElementById() method

getElementById() method selects or targets the HTML tag by it's id.

In this example getElementById() method is used to select the textarea element.

HTML Code

HTML Code is given below, in this code we have a textarea element and a button tag with onclick event.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Set Textarea Value</title>
</head>             
<body>
    
<textarea id="textarea"></textarea>
<button onclick="setTextareaValue()">Set Textarea Value</button>    
    
<script src="script.js"></script>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, In this code user defined function setTextareaValue() will set the value of textarea.

getElementById() method is used to target textarea tag.

value property will set the value of textarea.

The value we used is stored in variable.

<script>
function setTextareaValue()
{
    var textarea = document.getElementById('textarea');
    var value = 'New Value';
    textarea.value = value;
}
</script>

Set Dynamic Value of Textarea

To apply the dynamic value we can add input tag to the code and get the value from user through this tag and then set this value to textarea tag.

See the example below to set the dynamic value to textarea tag.

<script>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Set Textarea Dynamic Value</title>
</head>             
<body>
    
<textarea id="textarea"></textarea>
<br><br>    
<input type="text" placeholder="Enter Value" id="value">   
<br><br>    
<button onclick="setTextareaValue()">Set Textarea Value</button>    
    
<script>
function setTextareaValue()
{
    var textarea = document.getElementById('textarea');
    var value = document.getElementById('value').value;
    textarea.value = value;
}    
</script>    
</body>
</html>
</script>

Demo

Video Tutorial

Watch video tutorial on How To Set Textarea Tag Value using JavaScript.

How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript Change id of Element with JavaScript JavaScript Change li Text Color