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
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.).
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With