.innerHTML is not working properly (I think)

I use .innerHTML to add text to textarea . But if I start editing text in textarea own script, it stops working.

Here he is:

 (function($){ addPort = function(name) { switch(name) { case 'name1': var code = "text1"; break case 'name2': var code = "text2"; break case 'name3': var code = "text3"; break default: break } document.getElementById("codeArea").innerHTML += code; }; })(jQuery); 

Do not pay attention to jQuery. Some other features use it.

Here is textarea :

 <textarea id="codeArea" name="codeAreaPost"></textarea> 

Thanks.

+4
source share
2 answers

just replace innerHTML with value

 document.getElementById("codeArea").value 

not innerHTML

+7
source

innerHTML will parse the value as HTML and make the resulting DOM a child of your text area. This is not what you want. Try instead of the value attribute:

 document.getElementById("codeArea").value += code; 
+1
source

All Articles