How to get textarea value in jquery?

I have this form and am trying to get the value from the text area. for some reason he does not want.

<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); }); 

or jsfiddle

any ideas?

+48
javascript jquery textarea
May 08 '12 at 22:10
source share
8 answers

The textarea value is also taken using the val method:

 var message = $('textarea#message').val(); 
+93
May 08 '12 at 22:11
source share

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

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

Updated fiddle

+19
May 08 '12 at 22:11
source share

you should use val() instead of html()

 var message = $('#message').val(); 
+15
May 8 '12 at 10:12
source share

You do not need to use textarea#message

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

You can directly use

 var message = $('#message').val(); 
+4
May 3 '16 at 10:44
source share

You must verify that textarea is null before using val (), otherwise you will get an undefined error.

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

Then you can do everything with the message.

+2
Feb 26 '15 at 16:36
source share

in javascript:

 document.getElementById("message").value 
+2
Jul 08 '16 at 10:06
source share

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

$('textarea#message') can be 0 in length and then $('textarea#message').val() will be empty so that everything

+1
Jun 22 '15 at 21:22
source share

all values โ€‹โ€‹are always accepted with .val ();

see bello code

 var message = $('#message').val(); 
0
Feb 07 '16 at 2:39 on
source share



All Articles