How to copy a text to the clipboard using javascript?

 Solution: 

Step 1) Add input field with default value and Button:

<input type="text" id="myInput" value="JSHUB" >

<button type="submit" onClick="copyText()" > Copy Text</button>



Step 2) Write Javascript function to copy text to the clipboard:

function copyText(){

let copyText = document.getElementById('myInput')

copyText.select();

copyText.setSelectionRange(0, 99999) // For mobile

navigator.clipboard.writeText(copyText.value)

alert("copied text": copyText.value)



Post a Comment

0 Comments

Close Menu