0

Here I have a <div> and an <img> tag.

<div id='input' style="width: 500px; min-height: 40px; border: solid black;"></div><br>

<img id="img" style="width: 100px; min-height: 100px; border: gray;"></img>

Let's say I copied an image to my clipboard from my browser, and if I paste (ctrl+v) the image onto the <div>, the image appears at the <img> tag. What script is needed to achieve this?

jack
  • 305
  • 1
  • 8
  • 1
    Does this help? https://stackoverflow.com/questions/60503912/get-image-using-paste-from-the-browser – Chaska Oct 16 '20 at 05:38
  • 1
    In case you face some trouble moving this FIle to the ``, you can simply set `img.src = URL.createObjectURL( file )`. – Kaiido Oct 16 '20 at 05:50

1 Answers1

2

You can just use this onPaste event, extracting the first file in clipboard:


    document.getElementById('input').onpaste = (e) => {
      const eventData = window.clipboardData || e.clipboardData ;
      const theFile = eventData.files[ 0 ];
    };

    <div id='input' style="max-height: 500px; max-height: 40px; border: solid black;" contenteditable></div><br>

    <img id="img" style="width: 100px; min-height: 100px; border: gray;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/1004px-Google_%22G%22_Logo.svg.png">

In the example you can copy the logo and paste it in the div

Vincenzo Manto
  • 886
  • 1
  • 11
  • 30