Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't update textarea with javascript after writing to it manually

Tags:

I'm writing an online application where I save some texts to the database.
There are like 5 "textarea"-s and 5 "input type=text"-s.
I'm also implementing a search to easily find and edit the DB entries. A new select window is displayed(using prototype and ajax), and when clicking on any of its entries the below form gets populated (it's the same form that was used to add new results). Now here's where the problem arises....
If i add a new form or edit any existing one ALL TEXTAREA fields that were modified, get locked or something like it (only textareas, the inputs still work) ... They won't obey Javascripts .update anymore, so they don't change when i select the next entry .... OR AT LEAST THEY WONT IN FireFox (3.5.something). It works fine in IE, but since i'm a FF user and i wan't it work there as well i'm wondering if someone has come across any similar problems and solved it with ease.
The problem seem to go away when i call form.reset(), but that messes up some code generated select/option fields, besides i wan't the data to remain.

To me it looks like FF decided that the text I entered is more important than text javascript is trying to enter, so it overrides it... and i can't figure out why. At this point I'm blaming .update(), but i'm not sure how to do it otherwise.
The INPUT fields seem to have problems with .update (or it just didn't work for me), so i had to rewrite them to .value= (have tried .value with textareas as well, hoping that would fix anything, sadly with no avail).

So, has anyone any clues why this is happening and how to fix it, without having to reset the form on every step?

like image 496
RedFury Avatar asked Dec 18 '09 11:12

RedFury


People also ask

Does textarea have Onchange?

The onchange property of a Textarea element refers to an event handler function that is invoked when the user changes the value in the text area and then “commits” those changes by moving keyboard focus elsewhere.

How do you change textarea text?

Alternatively, you can use jQuery's . html() method, which uses the browser's innerHTML property to replace textarea content with the new content completely. Another good solution is to use the . val() method to set the text value of the textarea element.

How do you add text to a new line in textarea?

The text area tag defines a multi-line text input control. The size of a text area can be specified by the cols and rows attributes. By default, whenever we press “enter” or “shift+enter” it creates a new line in the text area.

Can textarea have default value?

Definition and Usage. The defaultValue property sets or returns the default value of a text area. Note: The default value of a text area is the text between the <textarea> and </textarea> tags.


2 Answers

I had this same problem. When HTML is parsing to DOM object, content of textarea is innetHTML of <textarea>

<textarea cols="10" rows="10">Content of textarea</textarea> 

Values document.getElementByTagName('textarea').innerHTML and document.getElementByTagName('textarea').value returns this same value Content of textarea

But after loading DOM object, changing innerHTML value doesn't change content of textarea box. A solution to change it is modifying value field.

document.getElementByTagName('textarea').value="New content of textarea"; 

or in jquery

$("textarea").val("New content of textarea"); 
like image 73
Skamielina Avatar answered Oct 20 '22 04:10

Skamielina


.value and .innerHTML seemed to fix the problem

like image 42
RedFury Avatar answered Oct 20 '22 04:10

RedFury