0

Hi below is my code,

<textarea id="textArea"></textarea>
<button class="btn" onclick="appendText()">Append Text</button>

<script>
function appendText()
{
    var textarea=document.getElementById('textArea');
    var t = document.createTextNode("Hello World \n"); 
    textarea.appendChild(t);
}
<script>

before type anything in textarea this code will append 'Hello World' to textarea, if anything manually typed and click button means this code will not append 'Hello World' to textarea, why append not working after typing in textarea ? Anyhelp appreciated

Thiyagarajan
  • 247
  • 3
  • 5
  • 15
  • maybe because it is onclick and not nclick so it doesn't register the event. all other ode is correct – mrdeadsven Sep 20 '18 at 06:48
  • Justinas , firstof all understand my question, I dont need to empty text area , If user type anything in textarea and hit button means the text will append with user typed data, but in this case its not append , before type anything in textarea It works – Thiyagarajan Sep 20 '18 at 06:49
  • @mrdeadsven , how to resolve this one ? – Thiyagarajan Sep 20 '18 at 06:50
  • @justinas and cerbrus , dont make this kind of nonsense thing ," How to change the Content of a – Thiyagarajan Sep 20 '18 at 06:56
  • in the button element change nclick to onclick :p. also the last script element needs to be a closure so . – mrdeadsven Sep 20 '18 at 06:57
  • sorry , by mistake I typed here as nclick , thats not issue, Now question updated – Thiyagarajan Sep 20 '18 at 07:06
  • function appendText() { var textarea = document.getElementById('textArea'); var t = "Hello World \n"; var lst = document.getElementById('textArea').value; document.getElementById('textArea').value = t + lst; } – safeena rasak Sep 20 '18 at 07:16
  • Ohh no need here to try something like that here you can simple use this: https://codepen.io/mrdeadsven/pen/BOMozx. – mrdeadsven Sep 20 '18 at 07:42

0 Answers0