Capturing text pasted into a text box using jQuery

I need to take a text area insert event using jQuery. I tried the following code but it does not work ...

$(document).ready(function() { $('#txtcomplaint').keyup(function() { TextCounter('txtcomplaint','counterComplaint', 1000 ); }) $('#txtcomplaint').onpaste(function() { alert() //TextCounter('txtcomplaint','counterComplaint', 1000 ); }) }); 
+13
jquery javascript-events copy-paste
Feb 11 2018-10-10T00
source share
4 answers

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); }); 
+21
Feb 11 '10 at 5:33
source share
 $('#txtcomplaint').bind('paste', function(e){ alert('pasting!') }); 

For an additional resource, look here .

+6
Feb 11 2018-10-10T00
source share

I finally got this to work for 1) input, 2) drag, 3) Ctrl-V, and 4) paste the mouse click from the context menu, but I had to attach the pastes and drop handlers to the document (where "taValue" is class of text fields I'm trying to control):

  $(document).on("paste drop", '.taValue', function (e) { myHandler.call(e.target, e); }); 

The keyup event on the text field has already fired. The next problem was that the paste and drop events fire before the text in the text field actually changes. In my case, I wanted to compare the new text with the source text. I resorted to setTimeout:

  function myHandler(e) { if (e && (e.type === "drop" || e.type === "paste")) { var me = this; setTimeout(function () { myHandler.call(me) }, 200); }... [more code to do the comparison] 

I hate using timeouts for such things, but it works (when I tried the 100 ms interval, it is not).

+1
Apr 09 '15 at 19:59
source share

This is the most useful solution:

 $("#item_name").bind("input change", function() {}); 

Perhaps the change is not significant.

0
Feb 09 '17 at 0:56
source share



All Articles