Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Text Area Value with jQuery

How could I change the value of a text area through jQuery. Here is a sample:

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

How would I do this through jQuery or just javascript? An answer is appreciated, thanks. I tried this:

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

However, that was just a shot in the dark.

like image 471
Jack Davis Avatar asked Apr 12 '12 21:04

Jack Davis


People also ask

How to change textarea value in jQuery?

To set or get the text value of input or textarea elements, use the . val() method. To get the value of a script element, use the . html() method."

How can get textarea value in jQuery using ID?

We can get the value of textarea in jQuery with the help of val() method . The val() method is used to get the values from the elements such as textarea, input and select. This method simply returns or sets the value attribute of the selected elements and is mostly used with the form elements.

How to get text from input jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How to get the value of a textarea using jQuery?

In jQuery, we use the val () method to get the value from any HTML element taking user input. If the HTML code for the textarea is the following: Where you are not using any attribute, then in jQuery, all you have to do is:

How do I change the text value of a text area?

Alternatively, you can use jQuery’s .html()method, which uses the browser’s innerHTMLproperty 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 1 2 3

How to replace textarea content with another element using jQuery?

Alternatively, you can use jQuery’s.html()method, which uses the browser’s innerHTMLproperty 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.

What is the difference between input and textarea in jQuery?

A textarea doesn't have a value attribute, while an input does. Agreed, but val () is setting it in this instance. They [jquery] must determine it's textarea type and set the text. textarea has a value (JavaScript) property just like a text input.


2 Answers

Assuming your textarea as

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

jquery code to update textarea value would be

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

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

like image 72
DG3 Avatar answered Sep 24 '22 18:09

DG3


jQuery

With jQuery you can use .val("Your new value") on textareas as well, even if the HTML-element doesn't have a value-attribute.

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

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

Plain JavaScript

With plain JS, it could be done like this:

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

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

like image 34
Christofer Eliasson Avatar answered Sep 23 '22 18:09

Christofer Eliasson