/    Sign up×
Community /Pin to ProfileBookmark

copy to clipboard?

is there a way to copy something to the “clipboard” using javascript?

for example, the user clicks on an icon next to an address displayed on the screen and that address is automatically copied to their clipboard. then they can paste it into another document or application.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Dec 01.2004 — The example below does not use an icon, it uses a text link. The links in this case are names of people. When a user clicks a name, the hidden info associated with that name is copied to the clipboard. Currently, the hidden info happens to be a street address, but can be any text.

<HTML>

<Head>

<Script Language=JavaScript>

var isSecond = false;
var isFirst = true;

function copyToClipboard(ID){

isRange = document.body.createControlRange();
isRange.add(document.all(ID))
if (isSecond)
{if (confirm('This will erase the Clipboard. nnDo you want to CONTINUE?')){isRange.execCommand("Copy");alert('The Clipboard was overwritten with your selection.')}}
else if (isFirst){isRange.execCommand("Copy");isFirst = false;
alert('Your selection was copied to the Clipboard.')}
isSecond = true;

}

</Script>

<Body>

<center>

<H3>Copy Selected and Hidden Data to Clipboard</H3>

<Table id='data1' Style='display:none'><TD>John Doe, 456 Any Street, Columbus, OH 44444</TD></Table>

<a href=javascript:copyToClipboard('data1') id='name1'>John Doe</a><br>

<Table id='data2' Style='display:none'><TD>Mary Roe, 123 Main Street, Avalon, PA 22222</TD></Table>

<a href=javascript:copyToClipboard('data2') id='name2'>Mary Roe</a><br>

<Table id='data3' Style='display:none'><TD>Henry Crow, 987 Easy Street, Paradise, CA 55555</TD></Table>

<a href=javascript:copyToClipboard('data3') id='name3'>Henry Crow</a>

</center>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@akoszauthorDec 01.2004 — that works great, thank you!
Copy linkTweet thisAlerts:
@Warren86Dec 01.2004 — You're welcome. Good luck with your project.
Copy linkTweet thisAlerts:
@sciguyryanDec 01.2004 — You may want to note that that code will only work in IE.


RyanJ
Copy linkTweet thisAlerts:
@akoszauthorDec 01.2004 — the IE thing doesn't matter for what i'm doing, but that's a good point.

here's another really simple way to do this. i just stumbled upon.


[CODE]
<HTML>
<HEAD>
<SCRIPT Language=JavaScript>

function copyToClipboard(yourtext)
{
window.clipboardData.setData("Text", yourtext);
}

</SCRIPT>
</HEAD>
<BODY>

<a href="##" onclick="copyToClipboard('this is your personal text')">John Doe</a><br>

</BODY>
</HTML>
[/CODE]
×

Success!

Help @akosz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...