Javascript Copy Text to Clipboard

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" οnclick="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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值