Results 1 to 3 of 3

Thread: [Help needed] Copy & Paste

  1. #1
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Help needed] Copy & Paste

    Hi guys,

    I'm pretty much a newbie when it comes to javascript and i'm still getting to grips with it all but i am in some need of some help.

    I'm doing a relatively simple project for a friend of mine and what he is wanting is a simple web page with several buttons that have different information on them.

    As you click each button it will automatically copy and then paste into a text area with each bit of information on a new line. I think I have grasped the copy and paste but I am a bit unsure as to how I copy each button and automatically paste into the text area.

    once the relevant information is pasted into the textbox - he then wants a button to copy all of that information which can be pasted into a different system.

    Here is my code that I have at the moment

    Code:
    <script type="text/javascript">
    
    function copy()
    
    {
    
       Copytext = document.getElementById("button").value;
    
       Copytext.execCommand("Copy");
    
    }
    
    function paste()
    
    { 
    
       document.form.textarea.focus();
    
       PasteText = document.form.textarea.createTextRange();
    
       PasteText.execCommand("Paste");
    
    } 
    
    </script>
    
    <body>
    
    <form name="form">
    
    <input type="button" onClick="copy()" value="Option 1" />
    </br>
    <input type="button" onClick="copy()" value="Option 2" />
    </br>
    <input type="button" onClick="copy()" value="Option 3" />
    </br>
    <input type="button" onClick="copy()" value="Option 4" />
    </br>
    <input type="button" onClick="copy()" value="Option 5" />
    </br>
    <input type="button" onClick="copy()" value="Option 6" />
    
    <p></p>
    
    <textarea id="textarea" cols="60" rows="5"></textarea>
    <br />
    <input type="button" onClick="copy()" value="Copy text to clipboard" />
    </form>
    Any help would be appreciated

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    There are lots of problems with copy/paste in different browsers for various security reasons. One bit of code may work in IE, but not other browsers. One piece of code might work in FF, but not IE, Chrome, etc.

    The best solution is to use zeroclipboard:
    http://code.google.com/p/zeroclipboard/

    I use it myself, and it is clean, cross-browser and easy to install/use.
    - Josh

  3. #3
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi JShor,

    Thanks for the advice but its not what im looking for. The wbe browser he will be using is IE anyway and ive managed to work it out using the following code

    Code:
    <script language="JavaScript">
    function clipboard()
    {
    var copy = document.getElementById('copy').createTextRange();
    copy.select();
    copy.execCommand("Copy");
    
    }
    
    function paste()
    {
    document.getElementById('text').focus();
    var paste = document.selection.createRange();
    paste.execCommand("Paste");
    }
    </script>
    
    
    
    <TEXTAREA ID="text" style="height:100px; width:250px;">
    </TEXTAREA>
    <BUTTON id="copy"onClick="clipboard(); paste();">Option 1</BUTTON>
    cheers anyway!

Bookmarks

Posting Permissions

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