search

Home  >  Q&A  >  body text

Error in pasting clipboard data API functionality in javascript Uncaught TypeError: Cannot read properties of undefined (reading ‘getData’)

Html button code is as follows, I tried a simple onclick function on the button and an event on the second button

<button onclick="myFunction()">Click me</button>
<button id="button">Paste</button>

Javascript code is as follows

function myFunction() {

      var clipboarddata =  window.event.clipboardData.getData('text');    
      console.log("paste value" + clipboarddata);
}

var button = document.getElementById("button");
button.addEventListener("click", function(event){
   alert(event.target);
   event.preventDefault();
   var clipboarddata =  window.event.clipboardData.getData('text');    
   console.log("paste value" + clipboarddata);
});

I get the same error both ways

P粉043432210P粉043432210258 days ago599

reply all(1)I'll reply

  • P粉232409069

    P粉2324090692024-04-05 09:15:58

    You can use navigator.clipboard.readText instead.

    
    sssccc
    

    reply
    0
  • Cancelreply