12

I have inputbox. When page load, i use mouse to right click inputbox and choose paste from contextmenu.

when text get pasted, which event to use to alert text instantly as soon as paste happens?

i use "input paste" but not work in IE

4 Answers4

44

You can bind these events like so:

    $(document).ready(function() {
        $("#Text1").bind('copy', function(e) {
            alert('copying text!');
        });
        $("#Text1").bind('paste', function(e) {
            alert('pasting text!');
        });
        $("#Text1").bind('cut', function(e) {
            alert('cut text!');
        });
    });
Buggabill
  • 13,726
  • 4
  • 42
  • 47
  • 1
    I would love to hear what the down voters' reasons were. Thanks. – Buggabill Feb 28 '13 at 21:30
  • Thank you Buggabill! this helped me out in a project I'm currently working on. – klewis Mar 22 '13 at 14:54
  • 1
    Very useful, though note these events are fired BEFORE the cut/paste action happens, so the value of a textbox will not be changed until after your alert() completes. I found it useful that onkeyup fires after a cut/paste/undo action. – Magnus Smith Apr 25 '13 at 13:32
  • The paste worked like a charm! and I second @MagnusSmith, I discovered the same thing. I just set a var that becomes true and in the keydown event, if that var is true, I run my code that looks at the text and then resets the paste to false until they paste again. :) THANKS! – cbloss793 Feb 03 '16 at 20:22
-1

A hack that would work most of the time would be to hook into the control's onchange while also storing the control's initial text in a separate variable. Any time the length of the new text is longer than the original text by more than one character, you can assume that something was pasted in. Obviously this wouldn't work if someone pasted in a one-character string, but people don't do that very often.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
-1

There is a better way now. Still not all browsers support this 2011, but this will change

oninput event handler

oninput fixes

more fixes

and other crossbrowser jquery solutions

Dan
  • 55,715
  • 40
  • 116
  • 154
  • http://whattheheadsaid.com/2010/09/effectively-detecting-user-input-in-javascript – Dan Oct 02 '11 at 13:32
-4

It was set an action with setInterval (javascript function), which checks every 200ms the content of input. If it is changed then the past or typing occurred.

Eugeniu Torica
  • 7,484
  • 12
  • 47
  • 62