Take the 2-minute tour ×

I have HTML textareas that 'sometimes' have 'undefined' in the value property - I can't work out what is causing it.

Background: I have a PHP script that generates lots of textareas on a page - each with a unique ID using a counting system. The textareas belong to different forms that appear on the page. It is like an email inbox with a 'quick reply' form under each email.

Sometimes, when the forms are submitted the corresponding textarea value comes up as 'undefined' instead of the actual value the user has typed into the field. Example code:

function quickReply(id)
  message = document.getElementById('textarea_' + id).value();

  //Send 'message' and other details to PHP for handling...

I have also tried calling the value with the jQuery equivalent:

$('textarea_' + id).val();

Most of the time everything works as expected, but sometimes the value comes up as 'undefined'. 'undefined' is then send to my PHP code in Javascript and ends up getting saved in the database as the 'reply' which is how the issue is being discovered.

What causes a textarea to have an 'undefined' value property? Anything I can try to resolve this?

share|improve this question
Need additional information and the context of this code. Could be anything from your browser to how you're calling quickReply(). How are you 'sending' to PHP? Is it undefined before its sent? –  paislee Dec 30 '11 at 0:52
When using vanilla JavaScript, use the property .value to get/set the value of a form field. It's only a method when using jQuery's .val() method. –  scunliffe Dec 30 '11 at 2:07

3 Answers 3

To select an item by Id in Jquery, don't forget to add the "#"

$('#textarea_' + id).val();
share|improve this answer

This is prob because your dom hasnt loaded yet is your tag in a document ready

$(document).ready(function() {
share|improve this answer

I was able to resolve this issue by using tags to enclose each form (with each form having a unique ID).

I believe it should be possible to consistently access the .value property of a textarea without it being contained within form tags, however it seems Internet Explorer needs it in form tags 'sometimes'.

Not the best answer - but it did solve the issue.

share|improve this answer

Your Answer


