This post will discuss how to set or update the value of textarea in JavaScript and jQuery.

1. Using JavaScript

With plain JavaScript, you can use either innerHTML, innerText or textContent to write text inside a textarea.

JS


HTML



Edit in JSFiddle

 
The innerText property will escape the specified text so that it will render correctly in HTML.
The innerHTML property won’t escape the text, which can lead to XSS attacks.
The textContent property offers better performance since its value is not parsed as HTML, and it also prevents XSS attacks.

2. Using jQuery

With jQuery, you can use the .text() method to replace the textarea’s text.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use jQuery’s .html() method, which uses the browser’s innerHTML property to replace textarea content with the new content completely.

 
Another good solution is to use the .val() method to set the text value of the textarea element.

JS


HTML



Edit in JSFiddle

 
To insert text at the end of a textarea, you can use the .append() method:

JS


HTML



Edit in JSFiddle

That’s all about setting or updating the value of textarea in JavaScript and jQuery.