Copy to Clipboard Button in Display

I created a button on the display form to copy a code to the clipboard. I copied the value to a text field first and have the following code int he button click. The alert shows the correct text but it will not write to the clipboard. I am using Microsoft Edge browser since that is the browser of choice for our organization.

var copyText = $(fd.field('Text1').$el).find('input')[0];

copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Copied the text: " + copyText.value);

Hello @IT.Joe,

Try out adding setTimeout function, like this:

setTimeout(function(){
	var copyText = $(fd.field('Text1').$el).find('input')[0];

	/* Select the text field */
	copyText.select(); 
	copyText.setSelectionRange(0, 99999); /*For mobile devices*/

	/* Copy the text inside the text field */
	document.execCommand("copy");

	/* Alert the copied text */
	alert("Copied the text: " + copyText.value);
}, 500);

I tried that nd it still will not copy to clipboard. It does show on the alert popup box though.

@IT.Joe,

Please share the code you are using and the screenshot of the display form.

@IT.Joe,

How do you pass the ID to Text1 field?

Is Text1 field displayed on the form or is it hidden?

Well using your questions I did have it hidden...so I simply unhid on button click then copied then re-hid it away which put it into the clipboard. Odd but it works.

@IT.Joe,

The field should be visible to copy its content to clipboard.