---
title: "Javascript Copy Text to Clipboard"
author: "yaofeng911"
date: "2010-08-23T16:33:20Z"
tags: ["HTML", "JavaScript", "Windows"]
categories: ["Web前端"]
slug: "javascript-copy-text-to-clipboard"
draft: false
lastmod: "2010-08-23T16:33:20Z"
id: 745194
url: "https://yaofeng911.iteye.com/blog/745194"
---

**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:

- setData
- getData
- clearData

 

**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.

 

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

 

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

 

 

```
<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.