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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question javascript Copy to clip board from text area

    Hi Friends,
    I need a copy to clipboard functionality from a text area using javascript. I use Zclip for the purpose,
    but it will not work as needed . I attach my working code along with this .. pls open index.html in browser
    and try it . Do any one can pls give a javascript solution to copy textarea content to clipboard...

    Thanks,
    Anes
    Attached Files Attached Files

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <textarea id="copytext1" rows = "12" cols = "24">
    This text will be copied onto the clipboard when you click the button below.
    </textarea>
    
    <textarea id="holdtext" style ="display:none;"></textarea>
    <br><br>
    <input type = "button" onClick="ClipBoard();" value = "Copy to Clipboard"</button> 
    
    <script type = "text/javascript">
    
    function ClipBoard() {
    holdtext.innerText = copytext1.innerText;
    Copied = holdtext.createTextRange();
    Copied.execCommand("RemoveFormat");
    Copied.execCommand("Copy");
    window.status="Contents copied to clipboard"
    setTimeout("window.status=''",2500);
    }
    
    </script>

    "Jason McGuire takes a look between his legs and likes what he sees." - Football commentator
    Last edited by Philip M; 11-18-2011 at 05:15 PM.

    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
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    If you are attempting that in any browser except Internet Explorer then it isn't going to work as IE is the only browser that provides such access to the clipboard.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by felgall View Post
    If you are attempting that in any browser except Internet Explorer then it isn't going to work as IE is the only browser that provides such access to the clipboard.
    A good reason for using IE!

    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.


  •  

    Posting Permissions

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