I have a series of buttons, and I'd like to copy the value of the button to the clipboard after the mouse has hovered over the button for two seconds.
function getButtonValue(obj)
{
document.getElementById(obj);
var dummy = document.createElement("input");
document.body.appendChild(dummy);
dummy.setAttribute('value', obj.value)
dummy.select();
document.execCommand("copy");
alert("\"" + obj.value + "\" has been copied to the clipboard.");
// document.body.removeChild(dummy);
} // function getButtonValue(obj)
<body>
<input type="button" id="button1" value="bite me 1" onmouseover="setTimeout(()=> getButtonValue(this), 2000);" />
<input type="text" />
<br />
<input type="button" id="button2" value="bite me 2" onmouseover="setTimeout(()=> getButtonValue(this), 2000);" />
<input type="text" />
<br />
<input type="button" id="button3" value="bite me 3" onmouseover="setTimeout(()=> getButtonValue(this), 2000);" />
<input type="text" />
<br />
</body>
I have commented out the "document.body.removeChild(dummy);" to show that the
document.body.appendChild(dummy);
dummy.setAttribute('value', obj.value);
dummy.select();
is functioning properly.
I don't get any console errors, but the button value is not getting copied to the clipboard.
The "setTimeout(()=> getButtonValue(this), 2000);" is also not working--2 seconds are passing before the "alert("\"" + obj.value + "\" has been copied to the clipboard.");" executes, but what I want is for the mouse to have to hover over the button for two seconds before the "dummy.setAttribute('value', obj.value)" is executed.
I'd like to avoid jQuery if possible and use only javascript, but will use jQuery if necessary.
I'm doing this on a Mac running 10.14.3, and using Safari 12.0.3, but get the same incorrect results on Chrome for Mac 72.0.3626.109, FireFox Quantum 65.0.1 and Opera 58.0.3135.65.
I'm also opening the page from "localhost".
Any help would be greatly appreciated.