Search code examples
jquerycopy-pastejquery-events

Capture text pasted into a textarea with JQuery


I have to take the paste event of a text area using JQuery. I have tried the following code but it is not working...

$(document).ready(function()
{ 
  $('#txtcomplaint').keyup(function()
  {  
     TextCounter('txtcomplaint','counterComplaint', 1000 ); 
  }) 
  $('#txtcomplaint').onpaste(function()
  {  
     alert()
     //TextCounter('txtcomplaint','counterComplaint', 1000 ); 
  }) 
});

Solution

  • You can do something like this

    $("#txtcomplaint").bind('paste', function(e) {
        var elem = $(this);
    
        setTimeout(function() {
            // gets the copied text after a specified time (100 milliseconds)
            var text = elem.val(); 
        }, 100);
    });