Top

Copy to Clipboard Javascript


Javascript Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  7.0 (of 2 scores)
Date Added  Mar 24, 2007
Last Updated  Mar 24, 2007
Tags  clipboard  copy 

Introduction

This will copy the text in a given field to the clipboard. Unfortunately it only works in IE, so Firefox users will still need to do the actual copy. But it'll at least select the text for Firefox users.

Here is how you set up your HTML:

Code:
<form name=myform>
<input type=text name=mytext value='This text will be copied'>
<input onclick="javascript:copyToClipboard('myform.mytext')" type="button" value="Send to Clipboard">
</form>


You can name the form and text box anything you'd like. Just be sure to send the proper parameters to the copyToClipboard function. You may also use a textarea like we do here for our snippets.

Grab the Code

function copyToClipboard(field)
{
    var content = eval("document."+field)
    content.focus()
    content.select()
    range = content.createTextRange()
    range.execCommand("Copy")
    window.status="Contents copied to clipboard"
    setTimeout("window.status=''",1800)
}

Comments

  (5)  RSS
M[n]M
Comments: 98
 
Javascript Snippet:  Copy to Clipboard Javascript
Posted on Oct 4, 2007 2:18 pm
nice thats what i was searching for!
markusPHP
Comments: 10
 
Javascript Snippet:  Copy to Clipboard Javascript
Posted on Jun 16, 2008 11:58 am
Works in Opera, you might add.
lionroar
Comments: 1
 
Javascript Snippet:  Copy to Clipboard Javascript
Posted on Dec 19, 2008 10:54 am
I had a question do you know how I can use this using the "textarea" tag. I would like to use it for copying html coding?
Hawkee
Comments: 1,062
 
Javascript Snippet:  Copy to Clipboard Javascript
Posted on Dec 19, 2008 11:46 am
lionroar, just use a textarea and reference the name of the textarea in the function call.
PATX
Comments: 388
 
Javascript Snippet:  Copy to Clipboard Javascript
Posted on May 5, 2009 5:46 pm
lol nice. i have one like this in python....

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom