I’ve just come over another time consuming and perfectly annoying issue, so I decided to drop this post.
My following jquery command was keep returning ‘undefined‘ value for no good reason.
var comment = $('textarea#' + telephone_nr).val();
<textarea id ="9801213">
As you guess, I used the telephone number value as my identifier to access the value in textarea item, but although I tried a number of methods, I didn’t manage to get the correct value. Weirdly, the internet is full of wrong information for this error. Many sites claim that textarea item has no .val() function or value attribute. (<<< This is highly wrong info. Textarea item has .val() function. )
In about half an hour, I tried the following piece of code because of some adviser website:
var comment = $("textarea").value;
The code above actually functioned (Pay attention! I didn’t say it worked, it only functioned). Certainly, this is not a solution for me. Because I don’t want to access the textarea item in an array, I want to be able to access these values dynamically.
After a few more attempts, I realised that the issue must be related to the id value. My project puts too much trust in telephone number value as primary key, and cause I created some other html items (like textbox, anchor, etc…), somehow I created a confusion while accessing the correct item. I still don’t know why this happened and I assure you there is only one textarea item with a unique identifier.
However, I solved the issue by altering the id attribute of my textarea items.
var comment = $('textarea#tel_' + telephone_nr).val();
<textarea id ="tel_9801213">
Making sure that id value has a unique value worked. That’s it.
I actually encountered this issue while implementing my last work. I’ve been building a restaurant reservation system and textarea field will keep the comments of the restaurant manager for his/her clients.
I will most likely encounter more trouble while working on this product, and naturally, there will be more posts. Stay tuned!
Sercan Leylek / OSLO