0

I have been trying to make some text within an input of my HTML unselectable in angular. I have refered to previous questions like make html text unselectable

The text currently is a code that I don't want my users to copy it before I allow it.

using this CSS works with most html elements, but not with my input.

I have tried with user-select none

Any ideas?

Thanks

.digital-code {
  -webkit-touch-callout: none !important;
  -webkit-user-select: none !important;
  -khtml-user-select: none !important;
  -moz-user-select: none !important;
  -ms-user-select: none !important;
  user-select: none !important;
}

<div fxLayout="row">
        <div fxLayout="column" fxFill>
          <mat-form-field appearance="fill">
            <mat-label>{{ digitalSealCodeTitle }}</mat-label>
            <input [readonly]="true" #sealCode matInput [value]="digitalSealCode" />
            <mat-icon matSuffix (click)="copyValue(sealCode)" matTooltip="Copy">file_copy</mat-icon>
          </mat-form-field>
        </div>
      </div>
Koni
  • 452
  • 1
  • 7
  • 19

4 Answers4

1

Try to change your css to:

input[readonly] {
    -webkit-touch-callout: none !important;
    -webkit-user-select: none !important;
    -khtml-user-select: none !important;
    -moz-user-select: none !important;
    -ms-user-select: none !important;
    user-select: none !important;
}
Denis O.
  • 1,841
  • 19
  • 37
0

You can use this simple js to prevent cut copy paste in your input field. digita-code should be the own class for your input field.

$('.digital-code').bind("cut copy paste",function(e) {
    e.preventDefault();
});
  • This didn't work for me. I'm still able to perform those actions. I don't have Jquery so this is what I tried.(changed ID for class btw) ngAfterViewInit() { var el = this.elementRef.nativeElement.querySelector('.digital-code'); el.addEventListener('cut copy paste click', (e) => e.preventDefault()); } – Koni Oct 02 '19 at 14:05
0

I was able to achieve this by using a JS approach It looks kinda dirty, but it will work for now. I will refactor it in a few days, but if someone wants to see the answer:

var digitalCodeElement = this.elementRef.nativeElement.querySelector('.digital-code');
      digitalCodeElement.addEventListener('contextmenu', e => e.preventDefault());
      digitalCodeElement.addEventListener('copy', e => e.preventDefault());
      digitalCodeElement.addEventListener('paste', e => e.preventDefault());
      digitalCodeElement.addEventListener('cut', e => e.preventDefault());
Koni
  • 452
  • 1
  • 7
  • 19
0

This should work (notice the *):

.row .column * {
    -moz-user-select: none;
     -ms-user-select: none;
         user-select: none;
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sergiu
  • 1,397
  • 1
  • 18
  • 33