I have search the web but they were either not working properly or it was way too long for me to understand and work it in with my own webpage. But accidentally I found this script that is short and does the job like no other I have ever seen. So I'd like to share it with you.
<html>
<head>
<title>Javascript for one-click highlight and copy to clipboard</title>
</head>
<script language='Javascript'>
function doact(d)
{
var doc = eval("document.readme."+d);
cp = doc.createTextRange();
doc.focus();
doc.select();
cp.execCommand("Copy");
}
</script>
<form name="readme">
<body>
<center>
<TEXTAREA name="text1" cols="30" rows="5">
Your basic HTML codes can be entered here.
Do not worry about HTML codes interfering with your regualr web page since the <TEXTAREA> cannot excute any codes at all.
</TEXTAREA><BR>
<input onclick="doact('text1')" type="button" value="Copy Code">
<BR><BR>
<TEXTAREA name="text2" cols="30" rows="20">
This TEXTAREA is to demonstrate the inclusion of the second parameter.
You can even skip lines or create list
1
2
3
4
5
</TEXTAREA><BR>
<input onclick="doact('text2')" type="button" value="Copy Code">
</center>
</body>
</html>
Couple of key elements you should be awareThis code is currently 100% compatible with IE. I'm still working on FireFox but I was told that I need to edit pref.js in "about" page. If you read this article Security Issues with Ctrl-C and Clipboard by cse-icons and made the change to your browser you need to undo or reverse the effect when executing this script. I uploaded to my personal webspace and made only the TRUSTED site to execute this script. You can see this script in action by visiting this page.
Few things I had issues when making this script working
1. Make sure that your <FORM name="*****"> is equal to var doc = eval("document.*****."+d);
2. <TEXTAREA name="text1"> is the same name in each input botton, i.e.
<input onclick="doact('text1')" type="button" value="Copy Code">
3. However it is written in the TEXTAREA is how it will appear when you Ctrl-V or paste to your page/notepad. So it will obey every format you include such as every space line, indents, tabs and even break of sentence or code.4. I have included the secondary TEXTAREA to demonstrate the ability to add additional code copy.
Since I found this code on the web I cannot take the credit for it. I merely took it apart and made it to work with what I intended. I couldn't find the author to this very short yet effective code. Perhaps one of you may know. This simple javascript was the shortest and most intuitive that I have seen so far. As soon as I get it working with FireFox I'll let you all know.














