Copy Selected Text

Submitted by:Andery Smith

Date added:09 July, 2013


Copy selected text by allowing user to simply click a button

Tags: copy text

Code Snippet:

function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');

And in the body of your web page, add the following where you want the text to appear:

<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to copy the highlighted text" name="btnCopy">
<textarea name="select1" rows="4" cols="45"></textarea>