ItSolutionStuff.com

How to Copy Text to Clipboard without Flash using JQuery?

By Hardik Savani • November 5, 2023
jQuery

We sometimes require to give function of copy some text by clicking on button or any link. There are several library can do copy text using flash player, but in this example i will do it without using flash player. In this example we will use

clipboard.js Jquery Plugin for copy to clipboard.

clipboard js provide us to simply copy to clipboard, you can simply do alert or message for after success coped. You can simply use by as written following code.

Code:

var clipboard = new Clipboard('.btn');


clipboard.on('success', function(e) {

e.clearSelection();

alert('Copy to Clipboard Successfully');

});


clipboard.on('error', function(e) {

alert('Something is wrong!');

});

Now i am going to share with you full example of clipboard js plugin. I use cdn for jquery and clipboard js that way you can run easily for testing. You can also see demo. So let's see bellow example:

Example:

<!DOCTYPE html>

<html>

<head>

<title>Copy to clipboard JS Example</title>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.16/clipboard.min.js"></script>

</head>

<body>


<!-- Target -->

<textarea id="bar" class="form-control" placeholder="Write Something for Copy"></textarea>


<!-- Trigger -->

<button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">

Cut to clipboard

</button>


<script type="text/javascript">

var clipboard = new Clipboard('.btn');


clipboard.on('success', function(e) {

e.clearSelection();

alert('Copy to Clipboard Successfully');

});


clipboard.on('error', function(e) {

alert('Something is wrong!');

});

</script>


</body>

</html>

For more information you can get from here : clipboard.js

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

JQuery Remove All Numbers from String Example

Read Now →

JQuery Moment Subtract Minutes to Datetime Example

Read Now →

JQuery Moment Subtract Month Example

Read Now →

How to Only Allow Numbers in a Text Box using jQuery?

Read Now →

JQuery Rotate Image Animation Example Code

Read Now →

JQuery - How to Push Specific Key and Value in Array?

Read Now →

How to Remove Duplicate Object from Array in JQuery?

Read Now →

How to Remove Duplicate Value from Array in JQuery?

Read Now →

PHP MySQL Confirmation Before Delete Record using Ajax Example

Read Now →

Bootstrap Lightbox Plugin Example Code

Read Now →

How to Get Instagram Followers Count using JQuery?

Read Now →

JQuery Chosen Multi Select Dropdown Example

Read Now →

JQuery Notification Popup Box using Toastr JS Example

Read Now →

JQuery Tag Input Plugin Example Code

Read Now →