Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable typing in textarea

I want to disable the user from typing in textarea, when word count limit is reached. But the user should be able to edit the text within the given limit.

I used attr.disabled property to disable from typing. But, this just blocks the user from typing and editing. I want to do this in angular.

Thanks in advance

like image 697
Anjana Avatar asked May 08 '20 08:05

Anjana


People also ask

How do I turn off textarea writing?

When present, it specifies that the text area should be disabled. A disabled text area is unusable and the text is not selectable (cannot be copied). The disabled attribute can be set to keep a user from using the text area until some other condition has been met (like selecting a checkbox, etc.).

How do I disable input field typing?

The disabled attribute is a boolean attribute. When present, it specifies that the <input> element should be disabled. A disabled input element is unusable and un-clickable.

How do I disable a text box in CSS?

You can't disable a textbox in CSS. Disabling it is not a presentational task, you will have to do this in the HTML markup using the disabled attribute.


1 Answers

demo put this in component to count spaces

check(){
    return this.test.split(" ").length-1;
    
  }

in html

<textarea 
    [(ngModel)]="test"
    name=".."
    placeholder="..."
    [maxLength]="10+check()"
   >
</textarea>

If you think function occurs performance loses then use (keypress) method and create attribute as ex_length=0; then in component.ts

keypress(){
    this.ex_length=this.test.split(" ").length-1; 
}

in html

<textarea 
    [(ngModel)]="test"
    name=".."
    placeholder="..."
    (keypress)="onkeypress()"
    [maxLength]="10+ex_length"
   >
</textarea> 

or as other way you can use custom pipe Demo

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'max'
})
export class MaxPipe implements PipeTransform {

  transform(value: any): any {
    return 10 + value.split(" ").length-1;
  }

}

an in html

<textarea 
    [(ngModel)]="test"
    name=".."
    placeholder="..."
    [maxLength]="test | max"
   >
</textarea> 
like image 62
mr. pc_coder Avatar answered Oct 19 '22 11:10

mr. pc_coder