2

I have a textarea which changes value dynamically. For instance on events like this:

$('textarea').click(function(){
   $(this).val('some value');
}); 

I would like to alert each time the value changes:

$('textarea').on("input propertychange",function(){
    alert('he');
});

This doesn't work. I also tried Jquery change and document.querySelector('textarea').addEventListener('propertychange' and other things nothing seems to function. What else can I try?

Example at JsFiddle

Youss
  • 4,196
  • 12
  • 55
  • 109
  • Working fine for me > http://jsfiddle.net/e4e38kmm/ – James G. Sep 25 '14 at 16:50
  • 1
    As far as I know programmatic changes do not trigger any events. You could do it manually : `$(this).val('some value').trigger("change");` – Stryner Sep 25 '14 at 16:52
  • @James G Add some value dynamically like so `$('textarea').val('value');` you will see it doesn't work – Youss Sep 25 '14 at 16:53
  • Triggering the event handler manually is the only way to do that, other than a horrible interval or something like that. – adeneo Sep 25 '14 at 16:53
  • @adeneo Yes intervals are horrible:) – Youss Sep 25 '14 at 16:55
  • @Quentin Hi, I don't understand the answer in the link, could you please tell me how to implement in the fiddle..? – Youss Sep 25 '14 at 17:00
  • 1
    @Youss — Ninsly has already given you the relevant code from that answer in a form that "just works" with your existing code. – Quentin Sep 25 '14 at 17:03
  • @Quentin Yes I got it now:) thank you very much – Youss Sep 25 '14 at 17:04

1 Answers1

0

If I recall, there is a keyup function you can use. Something like this:

$('#something').keyup(function(){
   //do what you want
  });

I also found this, and I think you can benefit from the answers here: Detecting input change in jQuery?

Community
  • 1
  • 1
BlueBaroo
  • 131
  • 2
  • 15
  • This is not dynamically added value but on keyup... – Youss Sep 25 '14 at 16:51
  • The keyup event will not detect when JavaScript changes the value of the field (nor will the input event if we pay attention to the link-only portion of your answer). Demo of this failing to work: http://jsfiddle.net/1h8j2dqm/ – Quentin Sep 25 '14 at 16:52