After a post back event javascript is not working

68 Views Asked by At

I am using the below code to copy content to clipboard. This is for asp.net core. But once a postback happens. this code is not working. What is the issue?

  function copy() {
  var txt = document.getElementById("result");
  navigator.clipboard.writeText(txt.innerText);
}
1

There are 1 best solutions below

0
Anonymouse Lovecat On

Well, some browser doesn't support this navigator.clipboard.writeText(), it's kinda like a Mozilla, I'll try make alternate this code for supporting most browsers. May help, if it is result of yours will be 'txt' only or copy text only. try this :

<input type="text" id="result" value="This is a text"/>
<input type="Button" id="b1" value="Copy Here" onClick="copy()"/>

<script>
function copy() {
/* return content of input field to variable text */
var txt = document.getElementById("result"); 
/* call function on button click */ 
txt.select(); 
document.execCommand("copy");
}

</script>