21

I have to use here jquery keyup to detect if user enter any text or not, but my problem is if user use paste the text using mouse, it won't be able to detect it, can you tell me the reason?

here is my source code:

$("textarea").keyup(function(){
        if($(this).val().length !== 0){
            $('#submit').attr('disabled',false);
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
Ben
  • 2,562
  • 8
  • 37
  • 62

2 Answers2

38

Apparently there are some situations where jQuery doesn't work for pasting. You might need to bind to several events as shown in following reference to catch users pasting contents accurately.

From Soliciting Fame - jQuery keyup vs bind - (from way-back machine)

// detect the change
$('input#myId').bind("change keyup input",function() { 
    // handle events here
});

UPDATE: bind was deprecated on Jquery 3.0 and was replaced with on

// detect the change
$('input#myId').on("change keyup input",function() { 
    // handle events here
});
ricks
  • 3,154
  • 31
  • 51
jwwishart
  • 2,865
  • 2
  • 23
  • 26
  • The link seems to be outdated, i.e. the domain has been taken into new use. I was thinking of replacing it with an Internet Archive link but wasn't sure if that was the best option. I guess it could be just removed completely (even if credit to the original source is then lost). – MJV Jul 07 '17 at 14:17
  • @MJV Thanks. I've replaced it with way-back machine link. – jwwishart Jul 14 '17 at 12:24
14

In this particular situation enough will be just bind to the input event:

$('textarea').bind("input", function() {});
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Ilia Rebane
  • 1,251
  • 1
  • 9
  • 10