2
<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"

Mohamad
  • 34,731
  • 32
  • 140
  • 219
Ahmad Farid
  • 14,398
  • 45
  • 96
  • 136

5 Answers5

6

Your 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 $

TNC
  • 5,388
  • 1
  • 26
  • 28
  • 2
    As Capsule noted, yes, you can start variables with $, and I think doing so is a good idea for storing jquery selections. – Matt Feb 22 '11 at 14:01
  • 1
    I think it's just a difference of opinion. I don't think starting string variables should use the $, but that's just my opinion. :) – TNC Feb 22 '11 at 14:09
  • The result of a `.val()` is not a jQuery selector object, so no `$` is more appropriate. – alxndr May 16 '13 at 20:50
2

You missed the # character in $('#metaSourceText')

Capsule
  • 6,118
  • 1
  • 20
  • 27
0

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.net/Ds4HC/1/

Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21
0

.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.com/2012/03/get-textarea-value-in-jquery.html

Avinash
  • 199
  • 1
  • 2
-2

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 #.

Community
  • 1
  • 1
David Precious
  • 6,544
  • 1
  • 24
  • 31
  • 3
    Wrong, a variable can actually start with $. It's not necessary but it won't break anything. – Capsule Feb 22 '11 at 13:57
  • 2
    Many people use the $ to indicate that its a jQuery object (or any other framework which uses the dollar sign). In this case it wouldn't be a jQuery object though, as `.val()` returns a string. – Nathan MacInnes Feb 22 '11 at 13:59
  • Ah yes, you're right - thanks for the correction. Related question: http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – David Precious Feb 22 '11 at 14:00