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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation javascript onclick copy to clipboard

    Hi,

    i have several links which i am trying to add an on click command to which copies a certain code to the clip board so the user can paste the code with out having to manually copy it first.

    i have this
    Code:
            $latest = simplexml_load_file('codes_ending_soon.xml');
            foreach ($latest->discount as $discount)
            {
                $code = $discount->id;
                $link = $discount->link;
                $store = $discount->storeName;
                $logo = $discount->logo;
                $details = $discount->details;
                if($code_count <=4)
                {?>
                    <div class="code_cont">
                    <div class="code_logo"><img src="<?php echo $logo?>" alt="<?php echo $store." code"?>" title="<?php echo $store." code"?>" height="32" /></div>
                    <a class="code" href="<?php echo $link?>" title="Click to see related product(s) / retailer and apply code" onclick="window.clipboardData.setData(<?php echo $code?>)"><?php echo $code?></a>
                    <div class="description"><?php echo $details?></div>
                    </div><?php
                    $code_count++;
                }
            }?>
    but when i click on the link nothing seems to copy at least in firefox anyway? can anyone help me please

    thanks
    Luke

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    50
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Copy to clip board using JavaScript

    This solution is that when they click on the textarea it highlights the entire area. They simply right click to copy

    Code:
    <html>
    <head runat="server">
        <title>Untitled Page</title>
    
        <script type="text/javascript">
        
            function Select(ID)
            {
                var control = document.getElementById(ID);
                var length = control.value.length;
                if (control.createTextRange)
                {
                    // IE
                    var range = control.createTextRange();
                    range.select();
                }
                else if (control.setSelectionRange)
                {
                    // FF
                    control.focus();
                    control.setSelectionRange(0, length);
                }
                return;
            }
            
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <textarea id="taCode" runat="server" cols="20" rows="10" readonly="readonly" style="overflow: scroll;
                    overflow-y: scroll; overflow-x: hidden; overflow: -moz-scrollbars-vertical;">This is the text that I want to be able to copy</textarea>
            </div>
        </form>
    </body>
    </html>
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!


  •  

    Posting Permissions

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