Textarea value update is not reflected on the page

I am trying to update the value of textarea using Javascript. The code is similar to:

console.warn("Before set, value is " + document.getElementById('myTextArea').value); document.getElementById('myTextArea').value = 'OMGWTFBBQ'; console.warn("After set, value is " + document.getElementById('myTextArea').value); 

Although the Firefox and Chrome consoles show that the value property has been updated, this does not affect the page itself.

A specific function is called from the onfocus handler of another element. The text feed itself is lazily initialized from the same method using:

 var messageText = document.createElement('textarea'); messageText.id = 'myTextArea'; someParent.appendChild(messageText); 

Obviously, if I use the console instead of running the script, it works.

+4
source share
1 answer

Hmm, after some testing, it seems that it should work the way you want it, so I'm not sure why it is not, you can try using the following instead and see if it solves your problem:

 console.warn("Before set, value is " + document.getElementById('myTextArea').value); document.getElementById('myTextArea').firstChild.textContent = 'OMGWTFBBQ'; console.warn("After set, value is " + document.getElementById('myTextArea').value); 

If this does not work for you, then something about your page is very strange :)

Try something extremely simple, like the following:

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script> $(function(){ console.warn("Before set, value is " + document.getElementById('myTextArea').value); document.getElementById('myTextArea').firstChild.textContent = 'OMGWTFBBQ'; console.warn("After set, value is " + document.getElementById('myTextArea').value); }); </script> </head> <body> <textarea id="myTextArea">hey</textarea> </body> </html> 

if this works, then the browser is not to blame, and you need to find the problem in your code. If this does not work, you can blame the browser :)

+2
source

Source: https://habr.com/ru/post/1335981/


All Articles