1

I want to detect when the content of a <textarea> element has changed. For changes done by the user, I can use solutions in these questions:

  1. How can I bind to the change event of a textarea in jQuery?
  2. Textarea onchange detection

which detects keypresses, pastes events etc. But what if the data is data-bound dynamically, for example using frameworks such as AngularJS, MeteorJS etc. I must then add a function to be ran in the frameworks alongside the data-binding functions.

Is there a Javascript or jQuery method / event listener that I can use to detect content change, without adding it in the framework?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dayuloli
  • 16,205
  • 16
  • 71
  • 126
  • Do the frameworks have an event that fires when an event is bound? If so listen for that and find all the elements and trigger the change event. – epascarello Dec 28 '14 at 15:04
  • @epascarello I'm sure different frameworks have different ways to do this. I am looking for a non-framework-specific method. Just because sometimes you inherit a project and deadlines are tight, it's not practical to RTFM for the framework, even though that might be the best solution. Sometimes I just want to eat dinner. – dayuloli Dec 28 '14 at 15:10
  • http://stackoverflow.com/questions/2844565/is-there-a-jquery-dom-change-listener – epascarello Dec 28 '14 at 15:14

1 Answers1

0

You can use setInterval This is an example with jQuery :

Html

 <textarea id="textarea"></textarea>
<a href="#" id="link">change text dynamicly</a>
<span id="status"></span>

Javascript

$(function(){
    $('#textarea').data('old',$('#textarea').val())
    $('#link').on('click',function(){
          $('#textarea').val('text changed dynamicly');
        return false;
    })
    setInterval(function() { 
        if($('#textarea').val() != $('#textarea').data('old')){
            $('#status').html('text of textarea is changed to : ' + $('#textarea').val())
        }
         $('#textarea').data('old',$('#textarea').val())
    }, 100);
})

this is a demo in jsfiddle

Abdallah Arffak
  • 1,193
  • 6
  • 13