2

Is it possible to know if text to an input field was added by typing or by using paste?

$('document').on("keyup", ".tfs", function(e) {
   alert("typed text");
})

$('.tfs').on('paste', function(e) {
   alert("paste text!");
});

EDIT: I'm looking for an solution which is not an inline code solution such as onpaste(). I'm need to do this using bind ( .on now ) as per my code above.

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 1
    possible duplicate of [Intercept paste event in Javascript](http://stackoverflow.com/questions/6035071/intercept-paste-event-in-javascript) – Evan Knowles May 20 '15 at 05:27
  • Your code shud work. – Zee May 20 '15 at 05:29
  • @EvanKnowles: I came across that post while googling. Inline `onpaste` is not the solution I'm seeking. I'm finding a solution using `bind` (`.on` now) – Becky May 20 '15 at 05:34
  • Most browsers support the `paste event` on forms and text inputs, but not all on window or document. So you have to bind it to the input element. [reference](http://quirksmode.org/dom/events/cutcopypaste.html) – Michel May 20 '15 at 05:35

2 Answers2

1

something may help

$(document).ready(function() {

    $("#textA").bind({
        copy : function(){
            $('span').text('copy behaviour detected!');
        },
        paste : function(){
            $('span').text('paste behaviour detected!');
        },
        cut : function(){
            $('span').text('cut behaviour detected!');
        }
    });

}); 

DEMO HERE

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
0

There are 's around document. Remove them or use this code -

$('.tfs').on("keypress", function(e) {
   alert("typed text");
})

$('.tfs').on('paste', function(e) {
   alert("paste text!");
});

FIDDLE

The keypress event will allow to check for characters not arrows or backspace etc.

Check this question

Community
  • 1
  • 1
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87