DEV Community

SumantaWeb
SumantaWeb

Posted on

ShoPo - How to copy a text to clipboard

ShoPo - How to copy a text to clipboard (using JS!)

ShoPo is a new posting session which is Short! (SHOrt POst).

Yeah! we can copy a text to clipboard at a click of a button!

so how to do it?

so do the following --
1)Add a click function to the button

using JavaScript way 1

<button onclick="function()">Copy!</button>
Enter fullscreen mode Exit fullscreen mode

using JavaScript way 2

<button id="copyButton">Copy!</button>

<!--- Just add a onClick event in js --->
Enter fullscreen mode Exit fullscreen mode

using Alpine

<button @click="function()">Copy!</button>
Enter fullscreen mode Exit fullscreen mode

2)Now code the function

function(){
  navigator.clipboard.writeText("text goes here!")
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)