10/6/2018 - 2:10 PM

Copy Selected Text

Often you may have some information on your page that your visitors might want to copy. You can make it easier for them by providing a mechanism that allows them to simply click a button to do so. First you need to paste this code into the head of your web page:

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>