Click here to Skip to main content
15,881,455 members
Articles / jQuery

Copying to Clipboard Using jQuery Without the Need of Flash

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
11 Mar 2016CPOL2 min read 13.9K   1  
Clipboard.js removes the flash component and just solves the issue elegantly - all you need to do is to import the script, assign a “data-clipboard-target” attribute to your HTML tag and write a small JavaScript snippet.

If you look online for a solution on how you will copy a certain text to clipboard, then most probably you will end up with solutions using Flash to do the task. While they all do a good job on solving the issue using Flash, I think it is not a smart idea as this product will not exist in the future or at least browsers will not support it so the solution is not future proof. Why not use jQuery or plain JavaScript? Well, you can write your own but why reinvent the wheel when someone already created a solution, let me present Clipbopard.js.

Clipboard.js removes the flash component and just solves the issue elegantly. All you need to do is to import the script, assign a “data-clipboard-target” attribute to your HTML tag and write a small JavaScript snippet. To demo this to you, let's assume you have an application that converts currency by typing your value in a text box and conversion rate shows on the another text box. When you click that text box, it will then copy to your clipboard and display a message that the event happens.

Image 1

Here is how I made it.

Let's say this is your text box (BTW I created my application using MVC):

HTML
<div class="row">
    <div class="col-md-6">
        From
        <div class="input-group">
            <div class="input-group-addon">$</div>
            @Html.EditorFor(model => model.AmountFrom, 
		new { htmlAttributes = new { @class = "form-control input-largest", 
		@step = "0.01", @type = "number" } })
        </div>
    </div>
    <div class="col-md-6">
        To
        <div class="input-group">
            <div class="input-group-addon">$</div>
            <input type="text" id="AmountTo" value="@Model.AmountTo" 
		class="form-control input-largest" readonly data-clipboard-action="copy" 
		data-clipboard-target="#AmountTo" />
        </div>
    </div>
</div>

If you noticed, I have an AmountTo and AmountFrom, AmountTo is our input and AmountFrom is our output where when we click it, the value is passed to the clipboard. The magic there happens in the attribute called “data-clipboard-target”.

We will also add a message box where a message will show up about the copy action:

HTML
<div class="row">
    <div class="col-md-6"><br />
    <span id="messageBox" class="text-success" 
	style="display: block; text-align: center"></span></div>
</div>

As far as the HTML Elements is concerned, you are all sorted. Now let’s go to the JavaScript/jQuery part:

HTML
<script src="~/Scripts/clipboard.min.js"></script>
<script>
    var clipboard = new Clipboard('#AmountTo');
    clipboard.on('success', function (e) {
        $("#messageBox").text("Amount Successfully Copied!").show().fadeOut(2000);
 
        e.clearSelection();
    });
 
    clipboard.on('error', function (e) {
        $("#messageBox").text("Error Copying Amount").show().fadeOut(2000);
    });
 
    $('#AmountFrom').click(function () {
        $("#AmountFrom").val("");
    });
 
</script>

So if you can see, we just imported the Clipboard.js, instantiate the Clipboard then assign actions to events when it's successful and if it encountered an error. Simple right? What's good with this is it works on all recent browsers but again with Internet Explorer, it gives you a message like this:

Image 2

If you want to see this in action, here is JSFiddle sample.

Up next is grabbing that clipboard data to paste in a textbox automatically when clicked, right now looks like browsers are preventing it as it's a security risk , but I will try to find or even make a solution, so watch this space.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
-- There are no messages in this forum --