Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Select all text and / or COPY text

    Hi all,

    Trying to figure out something simple I think. I have just some plain text on my webpage, it's not located in a text field or textarea whatever, just plan text. I want to put a button there to copy that text, or highlight it so people can copy it themselves.

    Google did not help me at all in finding anything, so I came here to ask for some help, if anyone knows how to accomplish this very simply, please let me know

    Thanks in advance!
    Last edited by Hardist; 03-15-2013 at 11:42 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Reading and modifying the clipboard from a webpage raises security and privacy concerns.
    Hence not all browsers allow access to the Windows clipboard. If you are willing to accept an Internet Explorer ONLY solution then please ask.

    A somewhat hacky solution which works in all browsers is to place the text to be copied in a prompt box and instruct the user to use CTL-C:-


    Code:
    <div id = "mytext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum. Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc. Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna mauris, pellentesque at ultricies vitae, fermentum vitae dolor. 
    
    </div>
    <br>
    <input type = "button" value = "Click to copy this text" onclick = "copyToClipboard()">
    
    
    <script type = "text/javascript">
    function copyToClipboard () {
    var text = document.getElementById("mytext").innerHTML;
    window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);
    }
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 03-15-2013 at 11:49 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Internet Explorer only would work. It's just for something simple and everyone that visits that page are using IE

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Hardist View Post
    Internet Explorer only would work. It's just for something simple and everyone that visits that page are using IE
    Why do your users not simply highlight the required text on the webpage and copy it themselves?

    Code:
    <span id="Data" value="This is the clipper function."></span><a href="javascript:clipper('Data')">clipper function</a><br>
    <span id="Data1" value="This is the clipper1 function."></span><a href="javascript:clipper('Data1')">clipper1 function</a><br>
    <span id="Data2" value="This is the clipper2 function."></span><a href="javascript:clipper('Data2')">clipper2 function</a><br>
    
    <script type="text/javascript">
    
    // IE ONLY
    
    
    function clipper(which) {
    var data = document.getElementById(which).value;
    window.clipboardData.setData("Text", data);
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Why do your users not simply highlight the required text on the webpage and copy it themselves?
    Not relevant

    Thanks for the replies, I think I can use this


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •