1

I run a website and there is an area to send messages with AJAX. I've noticed that some messages say undefined and therefore people can't use the message system. The following is the code used to get the value from the text area, whats wrong with it ? I'm thinking that some browsers don't accept it so how can I fix it? Thanks

<textarea id="message"></textarea>
$('textarea#message').val()
1
  • Works for me. I think the problem lies elsewhere in your code. Commented Aug 16, 2011 at 21:14

2 Answers 2

1

The val() method get the value attribut, but tag textarea has no value attribut. The text inside Textarea is saved inside the tags <textarea> and </textarea>, so you have to use $('textarea#message').text(). It should work.

Sign up to request clarification or add additional context in comments.

1 Comment

text() returns the original text inside the textarea, which is not the same as the current value (actually it should be the same as textarea.defaultValue). Except in IE due to bugs. There is almost never any good reason to use text() on a textarea. jQuery val() and the underlying JavaScript property textarea.value do exist and return the current value of the field.
0
  <textarea >some text</textarea >

   not like this

   <textarea value="some text"></textarea >

There is no value attribute

  <textarea id="message"></textarea>
  $('textarea#message').text();

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.