Copy Selected Text

Submitted by:Andery Smith

Date added:09 July, 2013

Category:JavaScript

Copy selected text by allowing user to simply click a button

Tags: copy text

Code Snippet:

<SCRIPT LANGUAGE="JavaScript">
function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>

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">
<p>
<textarea name="select1" rows="4" cols="45"></textarea>
</div>
</form>
 
 

Comments