X

LATEST #JQUERY NEWS

Copy Data to Clipboard Using jQuery


In this post, find jQuery solution to allow users to copy data to clipboard. This solution is quite handy when right click is disabled for your website and also text selection is also disabled. When text selection is disabled along with right click, it’s difficult for users to select the text and then copy it.

Here is the jQuery solution to copy data to clipboard using jQuery:

$(function() {</span>
   $('.allowCopy').click(function() {</span>
     $(this).focus();</span>
     $(this).select();</span>
     document.execCommand('copy');</span>
   });
});

The above jQuery code will allow click events for all HTML controls with a CSS class “allowCopy” and then select the text and execute document.execCommand(‘copy’); command which will copy the data. Below is HTML code:

<input readonly type="text" value="Click Me To Copy" class="allowCopy noselect">

Here is the jQuery code to disable right click on your website:

$(function(){</span>
   $(document).on("contextmenu",function(e){</span>
      e.preventDefault();</span>
   });
});

You can use following CSS class to disable text selection on your website:

.noselect {
  -webkit-touch-callout: none; 
  -webkit-user-select: none;   
  -khtml-user-select: none;    
  -moz-user-select: none;      
  -ms-user-select: none;      
  user-select: none;
}

Recently Published

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...

article image
»

How to Detect Browser Using jQuery

One of the more frustrating parts of creating a new web project is ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Use jQuery to Set Textarea Max Length

If you have a textarea field on any of your sites, it might be a good ...

getTable
»

15 Awesome jQuery Table Plugins

Tables are the most preferred way to display data because rows and ...

article image
»

How to Dynamically Open External Links in New Window

Any web developer will probably know that using the ...

article image
»

How to Use jQuery’s .not() Method

jQuery’s .not() method is a method that allows you to return ...