/    Sign up×
Community /Pin to ProfileBookmark

Hey,

I need a javascript that can allow a user to highlight text on a webpage and then click a button and the text that is selected would be copied to the clipboard.

Anysuggestions or help?

Maybe this could be used:

Description: This “demo” script does two things: 1) identifies the selected text on the page 2) copies this text to clipboard. As part of a larger application, it is useful wherever text needs to be rapidly and instantly copied.

[url]http://www.dynamicdrive.com/dynamicindex11/copytext.htm[/url]

[code=php]<script>

//Copytext to clipboard- by Gennero ([email protected])
//Submited to DynamicDrive.com
//Visit [url]http://www.dynamicdrive.com[/url] for this script

bBool=false
var copiedtext=””
var tempstore=””

function initiatecopy() {
bBool=true;
}

function copyit() {
if (bBool) {
tempstore=copiedtext
document.execCommand(“Copy”)
copiedtext=window.clipboardData.getData(“Text”);
if (tempstore!=copiedtext) {
alert(copiedtext);
}
bBool=false;
}
}

document.onselectionchange = initiatecopy
document.onmouseup = copyit

</script>[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@starrwriterJul 29.2004 — I don't understand the purpose of such a script. You can use the mouse to highlight text and copy it to the clipboard.
Copy linkTweet thisAlerts:
@Anthony2oo4authorJul 31.2004 — Yea, it automaticly copies it to the clipboard, but I don't want it to do that, I want the user to click a button to copy it to the clipboard
Copy linkTweet thisAlerts:
@starrwriterAug 01.2004 — [i]Originally posted by Anthony2oo4 [/i]

[B]Yea, it automaticly copies it to the clipboard, but I don't want it to do that, I want the user to click a button to copy it to the clipboard [/B][/QUOTE]


Why?
Copy linkTweet thisAlerts:
@HaganeNoKokoroAug 01.2004 — try
<i>
</i>&lt;html&gt;
&lt;head&gt;

&lt;script type="text/javascript"&gt;
function copy() {
var myRange=document.selection.createRange();
myRange.execCommand("Copy");
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

Hello there I am some text.
&lt;div id="d1"&gt;
hello I am some more text select me!
&lt;/div&gt;
&lt;input type=button onclick="copy()" value="Copy Selected Text"&gt;

&lt;/body&gt;
&lt;/html&gt;


Note: seems to work only in Internet Explorer.
Copy linkTweet thisAlerts:
@sciguyryanAug 01.2004 — I agree with HaganeNoKokoro.

HaganeNoKokoro

's script is the same as I use but, it will only work with IE.
×

Success!

Help @Anthony2oo4 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 5.22,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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