Javascript clipboardData Object provides the functionality to use windows clipboard to copy text using javascript function. You can use the member methods of clipboardData object supported by Javascript to copy text from the HTML document elements. clipboardData object supports 3 methods:

  1. setData
  2. getData
  3. clearData

1. setData method of javascript clipboardData object accepts two parameter values as argument. First takes the value as format of data and second parameter value as source of data or text that is to be copied. First parameter for format type can have two types of values one as Text and other as Url. setData method returns Boolean type value based on the successful execution to copy text on clipboard.

2. getData method of javascript clipboardData object accepts one parameter as format type of data stored on clipboard i.e. Text or Url.

3. clearData method of javascript clipboardData object clears the data stored on the clipboard. clearData method doesn’t take any parameter value to execute.

Javascript Copy Text to Clipboard Example

<script type="text/javascript" language="javascript">
    function copyText() {
        var copyDivText = document.getElementById('div1').innerText;

        var returnVal = window.clipboardData.setData('Text', copyDivText);

        alert(returnVal);

        document.getElementById('div2').innerText = window.clipboardData.getData('Text');
    }
</script>

<div id="div1">
    Copy this text to the clipboard using javascript
</div>

<br />

<input type="button" id="btn" value="copy" onclick="copyText()">

<br />

<div id="div2">
</div> 

Javascript copy text to clipboard uses the clipboardData objects that consume the system clipboard utility to store the data until new data is being copied on the system clipboard. You can paste the stored data on clipboard multiple times.

Continue to next tutorial: Javascript Open Window Print to learn how to open the printer dialog box using JavaScript print method.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.