Home >> Tutorials >> Javascript tutorials

his example will copy text on the page to the clipboard (then try using the menu to paste…)
Copy Example Code to your Clipboard


<html> <head> <script> function CopyToClipboard() { obj = null; if(document.getElementById) obj=document.getElementById('the_code'); if (obj) { window.clipboardData.setData('Text', obj.innerText); } } </script> </head> <body> <script> if(document.getElementById) document.write(" <a href='javascript:CopyToClipboard()'>Copy Example Code to your Clipboard</a>"); </script> <table border=1> <tr><td><span id='the_code'> This example will copy text in the table to the clipboard (then try using the menu to paste...) </span></td></tr></table></body> </html>         

   Explanation of code:
(1) When you click on the link Copy Example Code to your Clipboard function CopyToClipboard() will be called. In this function getElementById() method returns the first occurrence of the ID attribute value, the_code.
(2) Then setData method of clipboardData object assigns data in a specified format to the clipboardData object .

Page contents: itechies.net – Copy text To Clipboard