mdnba50
mdnba50

Reputation: 379

Exclude input from disabled copy and paste jQuery

I have this code to disable selecting text, copying and paste but it also prevents copying from input/textarea.

$('body').bind('cut copy paste', function(e) {
    e.preventDefault();
});

How do I enable copy/paste event listeners from textarea/inputs elements?

Upvotes: 1

Views: 237

Answers (1)

Agney
Agney

Reputation: 19224

You can achieve the desired effect only using CSS. Instead of disabling copy and paste using Javascript which will affect the user, you can use css attribute user-select

body {
  user-select: none;  //Disables selecting text
}
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Itaque cumque dolores ad eaque consectetur. Officia quaerat voluptatem laudantium nostrum, debitis modi omnis? Vero laudantium quos assumenda, hic voluptatibus quis illum.</p>
<input type="text" />

user-select is supported in all modern browsers except Opera Mini and UC Browser. https://caniuse.com/#feat=user-select-none

Upvotes: 1

Related Questions