最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read the contents of text area by jQuery? - Stack Overflow

programmeradmin5浏览0评论
<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 badges
Add a ment  | 

5 Answers 5

Reset to default 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 $

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

发布评论

评论列表(0)

  1. 暂无评论