0

I have a textarea tag. I want to be able to get its textContent after textContent changed?

<textarea id="xxx"> sameple text </textarea> // before
<textarea id="xxx"> sameple text and new text </textarea> // after

I want listen even when the textarea's content has changed.

Seth
  • 10,198
  • 10
  • 45
  • 68
Iner
  • 11
  • 5
  • 3
    Possible duplicate of [Textarea onchange detection](http://stackoverflow.com/questions/2823733/textarea-onchange-detection) – Manish Jun 09 '16 at 04:18

3 Answers3

1

you can put your script as

$(document).ready(function(){
  $("#xxx").change(function(){
     alert($("#xxx").val());
  });
});

if you put $("#xxx").html() it will give you before text but if you keep $("#xxx").val() it will give you changed value.

Codec
  • 271
  • 2
  • 16
0

You will need to use onkeyup and onchange for this. The onchange will prevent context-menu pasting, and the onkeyup will fire for every keystroke.

$(document).ready(function(){
       $(document).on('change','#xxx',function(){
             var newtext = $(this).text();
       });
});
Manish
  • 247
  • 1
  • 10
0

Use input event.

The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed.

$('#xxx').on('input', function() {
  console.log(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<textarea id="xxx">sameple text</textarea>
Rayon
  • 36,219
  • 4
  • 49
  • 76