Reputation: 2093
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:
//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 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?
Upvotes: 3
Views: 3954
Reputation: 2093
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.
Upvotes: 0
Reputation: 2253
To select an item by Id in Jquery, don't forget to add the "#"
$('#textarea_' + id).val();
Upvotes: 4
Reputation: 10260
This is prob because your dom hasnt loaded yet is your tag in a document ready
$(document).ready(function() {
$('textarea_' + id).val();
});
Upvotes: 4