Get textarea text with javascript or Jquery Get textarea text with javascript or Jquery jquery jquery

Get textarea text with javascript or Jquery


READING the <textarea>'s content:

var text1 = document.getElementById('myTextArea').value;     // plain JavaScriptvar text2 = $("#myTextArea").val();                          // jQuery

WRITING to the <textarea>':

document.getElementById('myTextArea').value = 'new value';   // plain JavaScript$("#myTextArea").val('new value');                           // jQuery

See DEMO JSFiddle here.


Do not use .html() or .innerHTML!

jQuery's .html() and JavaScript's .innerHTML should not be used, as they do not pick up changes to the textarea's text.

When the user types on the textarea, the .html() won't return the typed value, but the original one -- check demo fiddle above for an example.


To get the value from a textarea with an id you just have to do

Edited

$("#area1").val();

If you are having more than one element with the same id in the document then the HTML is invalid.


You could use val().

var value = $('#area1').val();$('#VAL_DISPLAY').html(value);