Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste text into textarea from clipboard when moveover using javaScript

Tags:

javascript

dom

I am trying to paste data from clipboard to textarea

<textarea id="mytextarea"></textarea>

<script type="text/javascript">
    tinymce.init({
        selector: '#mytextarea',
        plugins: "paste",
        paste_data_images: true
    });

    function handlePaste (e) {
        var clipboardData, pastedData;

        // Stop data actually being pasted into div
        e.stopPropagation();
        e.preventDefault();

        // Get pasted data via clipboard API
        clipboardData = e.clipboardData || window.clipboardData;
        pastedData = clipboardData.getData('Text');

        // Do whatever with pasteddata
        var paste = document.getElementById('mytextarea');
        paste.innerText = pastedData;
    }

    document.getElementById('mytextarea').addEventListener('mouseenter', handlePaste);
</script>

But nothing happening no error. I found many answers but i none solved my problem

like image 542
siva sandeep Avatar asked Apr 07 '26 12:04

siva sandeep


1 Answers

I believe in Chrome you can only access clipboardData when handling the paste event. So you would have to be listening for that event to access the clipboardData.

Change your event listener to listen for the paste event, and then you can do whatever you want with the clipboardData in the handler:

document.getElementById('mytextarea').addEventListener('paste', handlePaste);

You can see this in action in the following fiddle:

https://jsfiddle.net/x3v8bhhr/

I added a second textarea. When you try to paste into the first textarea, the clipboardData is intercepted, and placed in the second textarea instead.

like image 132
JoshG Avatar answered Apr 10 '26 00:04

JoshG