Web Analytics Made Easy -
StatCounter javascript onclick copy to clipboard - CodingForum

Announcement

Collapse
No announcement yet.

javascript onclick copy to clipboard

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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>
                    [COLOR="Red"]<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>[/COLOR]
                    <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
    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!

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎