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 Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...