how to get the value of a textarea in jquery?

泄露秘密 提交于 2019-11-26 20:27:23

Value of textarea is also taken with val method:

var message = $('textarea#message').val();

You need to use .val() for textarea as it is an element and not a wrapper. Try

$('textarea#message').val()

Updated fiddle

you should use val() instead of html()

var message = $('#message').val();

in javascript :

document.getElementById("message").value
SidTechs1

You don't need to use textarea#message

var message = $('textarea#message').val();

You can directly use

var message = $('#message').val();

You should check the textarea is null before you use val() otherwise, you will get undefined error.

if ($('textarea#message') != undefined) {
   var message = $('textarea#message').val();
}

Then, you could do whatever with message.

Zbyszek Swirski

$('textarea#message') cannot be undefined (if by $ you mean jQuery of course).

$('textarea#message') may be of length 0 and then $('textarea#message').val() would be empty that's all

Jawwad Ali Khan

You don't need to use .html(). You should go with .val().

From the doc of .val():

The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined.

var message = $('#message').val();

You can directly use

var message = $.trim($("#message").val());

Read more @ Get the Value of TextArea using the jQuery Val () Method

Ajay

You can also get value by name instead of id like this:

var message = $('textarea:input[name=message]').val();
محمد المسلم

all Values is always taken with .val().

see the code bellow:

var message = $('#message').val();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!