Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the value of a textarea in jquery?

i have this form and im trying to get the value from the text area. for some reason it doesn't want to.

<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?

like image 314
Patrioticcow Avatar asked May 08 '12 22:05

Patrioticcow


People also ask

How to get value from textarea?

Use the value property to get the value of a textarea, e.g. const value = textarea. value . The value property can be used to read and set the value of a textarea element. If the textarea is empty, an empty string is returned.

Can textarea have value?

From the MDN documentation: " <textarea> does not support the value attribute".

How do I display textarea content in HTML?

Use the <textarea> tag to show a text area. The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows. Specifies that on page load the text area should automatically get focus.


2 Answers

Value of textarea is also taken with val method:

var message = $('textarea#message').val(); console.log(message);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea cols="60" rows="5" id="message" name="message"> Hello,  world! </textarea>
like image 88
VisioN Avatar answered Sep 28 '22 15:09

VisioN


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

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

Updated fiddle

like image 32
Selvakumar Arumugam Avatar answered Sep 28 '22 17:09

Selvakumar Arumugam