0

I managed to narrow it down to a very small fiddle:

https://jsfiddle.net/wn7aLf3o/4/

Basically when you click on 'append' it should add an "X" to the textarea. That works great, until I decide to focus the textarea and type some stuff manually. Then I can't append X anymore.

Tested in Chrome and Firefox. What am I doing wrong?

HTML:

<div id="append">append</div>
<textarea id="text"></textarea>

JQuery:

$(function(){
    $(document).on('click', '#append', function(){
    $('#text').append(" X");
  });
});
user2015253
  • 1,263
  • 4
  • 14
  • 25
  • Possible duplicate of [How to append text to ' – tao Aug 09 '18 at 12:18

2 Answers2

6

You need to use val instead:

$(function(){
 $(document).on('click', '#append', function(){
   $('#text').val($('#text').val() + " X");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="append">append</div>

<textarea id="text"></textarea>
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
-1

Updated fiddle at https://jsfiddle.net/wn7aLf3o/21/. You don't have to append an element in a textrarea. Append is used to add an HTML element, however you want to modify the text of textarea, so use Val().

$(function(){
    $(document).on('click', '#append', function(){
    $('#text').val( $('#text').val() + " X") ;
  });
});
Charu Maheshwari
  • 1,583
  • 1
  • 8
  • 9