<textarea id="metaSourceText" name='key' style="width:100%" class="text ui-widget-content ui-corner-all" rows="1"></textarea>
I tried
$metaSourceValue = $('metaSourceText').val();
alert($metaSourceValue);
But it shows "undefined"
<textarea id="metaSourceText" name='key' style="width:100%" class="text ui-widget-content ui-corner-all" rows="1"></textarea>
I tried
$metaSourceValue = $('metaSourceText').val();
alert($metaSourceValue);
But it shows "undefined"
Share Improve this question edited Sep 13, 2012 at 15:48 Mohamad 35.4k34 gold badges143 silver badges220 bronze badges asked Feb 22, 2011 at 13:53 Ahmad FaridAhmad Farid 14.8k45 gold badges98 silver badges138 bronze badges5 Answers
Reset to default 6Your code just needs to be tweaked, to something like this:
var metaSourceValue = $('#metaSourceText').val();
alert(metaSourceValue);
you were missing the hash before metaSourceText, signaling an ID to jQuery. And you typically don't want to start variables with $
You missed the # character in $('#metaSourceText')
.text() method will also give you value of textarea. In ready() state you can either get object of textarea using class selector or id selector.
$(document).ready(function () {
$("#submitbtn").click(function () {
var textAreaValue = $("#txtMessage").text();
alert(textAreaValue);
});
});
Check sample here: http://www.codegateway./2012/03/get-textarea-value-in-jquery.html
Please define the selector with '#' prefix as it is an ID you are referring. In your case, it refers a DOM element of type metaSourceText which really does not exists..
To get a value of this text area: you can use .text() or val();
$(function(){
var textareaContent = $('#metaSourceText').text();
alert(textareaContent);
});
fiddle link:http://jsfiddle/Ds4HC/1/
Javascript variables don't start with $
. EDIT: They can, but usually do not. See
Why would a JavaScript variable start with a dollar sign?)
You want to try:
var metaSourceValue = $('#metaSourceText').val();
alert(metaSourceValue);
The $(...)
used by jQuery is a shortcut to the jQuery
function.
Also, as others mentioned, you need $('#metaSourceText')
if you're trying to reference the textarea by id - you were missing the #
.