cómo obtener el valor de un textarea en jquery?


Tengo este formulario e intento obtener el valor del área de texto. por alguna razón no quiere.

<form action="/profile/index/sendmessage" method="post" enctype="application/x-www-form-urlencoded">
    <div class="upload_form">
        <dt id="message-label"><label class="optional" for="message">Enter Message</label></dt>
        <dd id="message-element">
        <textarea cols="60" rows="5" id="message" name="message"></textarea></dd>
        <dt id="id-label">&nbsp;</dt>
        <dd id="id-element">
        <input type="hidden" id="id" value="145198" name="id"></dd>
        <dt id="send_message-label">&nbsp;</dt>
        <dd id="send_message-element">
        <input type="submit" class="sendamessage" value="Send" id="send_message" name="send_message"></dd>
    </div>
</form>


$("input.sendamessage").click(function(event) {
    event.preventDefault();

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

    console.log(message + '-' + id);
});

O jsfiddle

¿Alguna idea?

Author: Patrioticcow, 2012-05-09

11 answers

El valor de textarea también se toma con el método val:

var message = $('textarea#message').val();
 107
Author: VisioN,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-08 22:11:18

Necesita usar .val() para textarea ya que es un elemento y no una envoltura. Try

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

Fiddle actualizado

 19
Author: Selvakumar Arumugam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-08 22:11:36

Debes usar val() en lugar de html()

var message = $('#message').val();
 15
Author: undefined,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-21 02:32:47

En javascript:

document.getElementById("message").value
 4
Author: Saurabh Chandra Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-08 10:06:21

No necesitas usar textarea#message

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

Puede usar directamente

var message = $('#message').val();
 4
Author: SidTechs1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-19 12:53:52

Debe comprobar que el área de texto es null antes de usar val() de lo contrario, obtendrá un error indefinido.

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

Entonces, podrías hacer lo que sea con el mensaje.

 3
Author: Wuji,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-26 16:36:13

$('textarea#message') no puede ser indefinido (si por $ se refiere a jQuery, por supuesto).

$('textarea#message') puede ser de longitud 0 y luego $('textarea#message').val() estaría vacío eso es todo

 1
Author: Zbyszek Swirski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-22 21:40:39

Puede usar directamente

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

Read more @ Obtenga el valor de TextArea usando el método jQuery Val ()

 1
Author: jonathan klevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-27 13:01:35

Todos los valores se toman siempre con .val();

Ver el código bello

var message = $('#message').val();
 0
Author: محمد المسلم,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-07 14:39:49

No necesitas usar .html(). Deberías ir con .val().

Del doc de .val():

El método .val() se usa principalmente para obtener los valores de elementos de formulario como input, select y textarea. Cuando se llama a una colección vacía, devuelve undefined.

var message = $('#message').val();
 0
Author: Jawwad Ali Khan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-08 08:06:30

También puede obtener el valor por nombre en lugar de id de esta manera:

var message = $('textarea:input[name=message]').val();
 0
Author: Ajay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-08 08:06:56