Friday, 30 November 2012

Copy Selected Text Using Javascript


Copy Selected Text Using Javascript


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: 


<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>


No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top