I have taught myself HTML and CSS code but I'm still finding JavaScript very hard to understand, now to my problem. I work for a small support group and created a stand alone webpages that has links to several other stand alone webpages. quite often we have to use the same commands in our systems so I made a webpage that would copy text to clipboard using the following code:
<html>
<head>
<title>Electrical Application Support</title>
<link rel="stylesheet" type="text/css" href="CSS/elec-styles.CSS'>
<style>
html,body,h1,h2,h3,h4,h5,h6 {font-family: "Roboto", sans-serif}
</style>
</head>
<body>
<h1 class="w3-text-teal w3-center">Password Reset</h1>
<button onClick="ClipBoard(copytext);">Copy</button><span ID="copytext">text 1</span><br>
<button onClick="ClipBoard(copytext1);">Copy</button><span ID="copytext1">text 2</span><br>
<button onClick="ClipBoard(copytext2);">Copy</button><span ID="copytext2">text 3</span><br>
<button onClick="ClipBoard(copytext3);">Copy</button><span ID="copytext3">text 4</span><b>(make sure the account is theirs)</b><br>
<button onClick="ClipBoard(copytext4);">Copy</button><span ID="copytext4">text 5</span><br><br>
<b>If they have a disuser flag then use this:</b><br><br>
<button onClick="ClipBoard(copytext5);">Copy</button><span ID="copytext5">text 6</span>
<textarea ID="holdtext" STYLE="display:none;"></textarea>
<script language="JavaScript">
function ClipBoard(ct) {
holdtext.innerText = ct.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}
</script>
</body>
</html>
But we have had to update the webpage and have to switch off the compatibility mode so the webpage displays properly but now this JavaScript doesn't work.
I did find this code which does work with the compatibility mode switched off:
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
});
with:
<p>
<textarea class="js-copytextarea">Hello I'm some text</textarea>
</p>
<p>
<button class="js-textareacopybtn">Copy Textarea</button>
</p>
from this post on this website
but I'm not knowledgeable enough to tweak it for use with several different text on the webpage (need up 14 commands) and also what can I swap the textarea to? Used <span>
& <div
> and the function doesn't work after the change.