4

I have this paste event like so:

$("body").bind("paste", function (e) {

        var pastedData = e.originalEvent.clipboardData.getData('text');

        console.log(pastedData);

});

Which is working great! However I also want the option get clipboardData via a click event like so:

$("#clipboard").on("click", function (e) {

        $("body").trigger("paste");

    });

But when I try this, I get this error:

Cannot read property 'clipboardData' of undefined

Is there away to get clipboardData via click event? Or to trigger the paste event via click event?

user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

1

This code may work for you. Please let me know that is this solution is that you wanted!

Working fiddle : https://jsfiddle.net/as6c3pLz/1/

html

<input id="trigger">
<button id="click" >CLICK TO PASTE</button>


Jquery

$('#click').click(function(){
    navigator.clipboard.readText().then(text => {
        // use text as a variable, here text = 'clipboard text'
        $('#trigger').val(text);
    });
});



NB : However it may not work for every event
eg. for mouseover, mouseleave it's not working
But currently working with click, keyup, keydown

Remember : (As David Bray said) For some cases using paste may regarded as a violation as user can copy many important informations and that can be stolen. So, please do not use it, if you need this for silly reason. And also, for further versions of browsers it might not be supported.

Jawad Ahbab
  • 1,462
  • 1
  • 19
  • 31
0

when the user uses the key combination (ctrl+v) they are delivering the clipboard to the browser, otherwise it's the users private data ... credit card number, lovers address etc ...

The answer is no - it's a security problem - it's explained quite well here:

David Bray
  • 566
  • 1
  • 3
  • 15