Changing the value of a text area using jQuery

How can I change the value of a text area via jQuery. Here is an example:

var content = "Hello World"; //Code to set the value of the text area to content here. 

How do I do this through jQuery or just javascript? The answer is welcome, thanks. I tried this:

 var txtArea = document.getElementById('aTextArea'); txtArea.value = rolls; 

However, it was just a shot in the dark.

+8
javascript jquery html
source share
3 answers

Assuming your text box

 <textarea id="textarea" rows="4" cols="50"> sample textarea </textarea>​​​​​​ 

The jquery code to update the textarea value will be

  ​$(document).ready(function(){ var content = "Hello World"; $("#textarea").val(content); });​ 

jsfiddle: http://jsfiddle.net/bhatlx/PuWH4/1/

+12
source share

JQuery

With jQuery, you can use .val("Your new value") in text areas, even if the HTML element does not have a value attribute.

 $(function(){ $("#id-of-textarea").val("test"); });​ 

Example: http://jsfiddle.net/sfxTt/

Plain javascript

With simple JS, this can be done as follows:

 document.getElementById('id-of-textarea').value = "Hello"; 

Example: http://jsfiddle.net/sfxTt/1/

+3
source share
 jQuery("textarea[name='message_text_reply']").val('Holla') 

This works very well for me, where the name of the input attribute is message_text_reply , the parameter $("#message_text_reply") does not work.

+1
source share

All Articles