javascript - HTML textarea value is coming up as 'undefined' - Stack Overflow

I have HTML textareas that 'sometimes' have 'undefined' in the value property - I c

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 es up as 'undefined' instead of the actual value the user has typed into the field. Example code:

//Javascript
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 es 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?

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 es up as 'undefined' instead of the actual value the user has typed into the field. Example code:

//Javascript
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 es 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 asked Dec 30, 2011 at 0:47 DanDan 2,0933 gold badges22 silver badges30 bronze badges 2
  • 1 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? – calebds Commented Dec 30, 2011 at 0:52
  • 1 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 Commented Dec 30, 2011 at 2:07
Add a ment  | 

3 Answers 3

Reset to default 4

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

$(document).ready(function() {
   $('textarea_' + id).val();
});

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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744174902a4561705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信