Jquery keyup ignores immutable characters

I am trying to fire an event when the content div is changed using keyup. Unfortunately, this also captures things like moving the cursor with the arrow keys. I just want him to detect the actual changes.

I am reading this question that seems similar, but the solution there does not work for things like linebreaks. Ideas?

+3
source share
1 answer

One way is to cache the length outside the handler, and then compare the current length of the text field with the cached length. If they differ from each other, you can take the key to which the added content was added to the text field. Hope this helps.

var textarea = $('textarea').get(0),
    length = $(textarea).val().length;

$(textarea).keyup(function() {
   if($(this).val().length != length) {

     //do something


     //cache new length
     length = $(this).val().length();
   }
});
+1
source

All Articles