Return to Snippet

Revision: 62290
at February 13, 2013 20:11 by apphp-snippets

Initial Code
<script language="javascript">
function CopyText(el){
    var selectedText = "";
        selectedText = window.getSelection();
    }else if (document.getSelection){
        selectedText = document.getSelection();
    }else if (document.selection){
        selectedText = document.selection.createRange().text;
    if(selectedText != ""){
        selectedText = selectedText.toString();
        el.value = selectedText;
        }else {
        alert("Select a text in the page and then press this button!");
Select any part of this text to copy it...
<form name="frmCopyText">
<textarea name="txtSelect" rows="4" cols="45"></textarea><br>
<input onclick="CopyText(this.form.txtSelect)"
       value="Press to copy the highlighted text"

Initial URL

Initial Description
Sometimes you have some information on your page and your visitors might want to copy it. The easiest way is to provide a mechanism that allows them to simply click a button to do so. You have to paste this code into the head of your web page:

Initial Title
Copy Selected Text in JavaScript

Initial Tags
javascript, text

Initial Language