0

I have a <textarea> that is getting updated from a form with jquery. I might never actually enter anything into the textarea directly.

But I need to know how I can monitor changes to the textarea that aren't directly from direct input on the textarea?

I can use

$('#myTextbox').on('input', function() {
    // do something
});

to know when the user is editing the myTextbox.

I tried

$('#myTextbox').on('change', function () {
  // do something
});

but that didn't work either.

Is this possible?

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • Possible duplicate of [JQuery detecting Programatic change event](https://stackoverflow.com/questions/359087/jquery-detecting-programatic-change-event) – JJJ Dec 05 '17 at 19:55
  • Is it two separate forms? What I mean is, is the form that sends data to the text area a different form all together that is sending the data? – AndyWarren Dec 05 '17 at 19:58
  • Also, do you need real time monitoring, or could you poll it like every few seconds to check for change? – AndyWarren Dec 05 '17 at 20:03

2 Answers2

1

You can try using interval or timeout like this

var searchValue = $('#myTextbox').val();

function checkSearchChanged() {
    var currentValue = $('#myTextbox').val();
    if ((currentValue) && currentValue != searchValue && currentValue != '') {
        searchValue = $('#myTextbox').val();
       console.log(searchValue)
    }
    setTimeout(checkSearchChanged, 0.1);
}

$(function () {
    setTimeout(checkSearchChanged, 0.1);
});

checkout working plunker here

  • I was thinking along these lines too, although I was think `setInterval()` would be more appropriate. `.01` is pretty fast checking too, not sure if its needed to poll it so many times. That would be 1/100th of a millisecond if I'm not mistaken. – AndyWarren Dec 05 '17 at 20:26
  • 0.1 is below the minimum timer resolution. It's exactly the same as setting it to 1. – JJJ Dec 05 '17 at 21:43
-1

Just use keyboard command to check to see if something is happening:

$('#myTextbox').on('keydown', function () {
  // do something
});

That way if someone ever types into it you will know. Else if you are trying to find out indirectly, you can use something that will check whenever an input is being used, so like:

$('input').on('keydown', function () {
  var checkDiv = $('#myTextbox').val();
  if(checkDiv != ""){
      // do something
  }
});
Keith
  • 4,059
  • 2
  • 32
  • 56
  • 1
    They aren't trying to watch for a person typing into it, but rather when the ` – AndyWarren Dec 05 '17 at 19:56