0

I wanted to know how to add text to the input text and then copy it to the clipboard. That added text is fixed. For example, the fixed text is abc; Now according to the following code, I enter the url and with the code(that i don't konw and my programming is zero!), the fixed text is added to before the url and then copied to the clipboard abc (a text constant) + url(is varibal) --> result = abc url ---> copied to clipboard

thanks a lot

<html>
<body>

<p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>

<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>

<script>
function myFunction() {
  var copyText = document.getElementById("myInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}
</script>

</body>
</html> ```
  • Set the fixed text and the variable together to the value of the input before selecting the input text. (`copyText.value = 'abc ' + url`). – Teemu Mar 25 '21 at 20:41
  • 1
    Does this answer your question? [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – Beller Mar 25 '21 at 20:41
  • @Teemu Hi there ! at the first thank you , your descriptin was a little short ! but i found it ! i just add this : `copyText.value = 'abc ' + copyText.value` after `var copyText = document.getElementById("myInput");` . again thanks :) – Hi French Mar 26 '21 at 06:12

0 Answers0