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
»

jQuery Snippets: How to Add/Remove Classes After Delay

In your projects, there can often be instances of wanting to change ...

article image
»

jQuery Snippets: Back to Top Button

One common way to use jQuery is to add a useful and functional ...

article image
»

jQuery .on() Method: the Issue of Dynamically Added Elements

The jQuery .on() method is the recommended way for attaching events ...

article image
»

How to Use jQuery to Test CSS3 Browser Support

The tricky thing about using CSS properties (especially newer ones ...

article image
»

jQuery Snippets: Check/Un-Check All Function

This easy-to-implement jQuery code snippet is super useful, and can ...

article image
»

Useful jQuery Code Snippets for Handling CSS

If you’re not already familiar with code snippets, they are ...

article image
»

Using Method Chaining in jQuery

Using method chaining in jQuery helps to ensure that your code stays ...

article image
»

jQuery’s .children() Method

Just like in CSS, it’s possible to use jQuery to select the ...

article image
»

jQuery’s .click() Method

With jQuery, it’s super easy to make something happen as a result ...