How can I disable the button if the input is empty, and enable it when the user start typing in the input, i tried that:
<input #new_field type="text" autocomplete="off" />
<p-button (onClick)="saveNewField(new_field.value)" [disabled]="new_field.value ==''" label="Save"></p-button>
and also this:
<input [(ngModel)]="searchText" size="30" autocomplete="off" />
<p-button (onClick)="saveNewField(searchText)" [disabled]="!searchText" label="Save"></p-button>
Enable / Disable submit button 1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.
The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).
If the user has not typed anything, then the text field will be equal ( === ) to the empty string and the button will remain disabled (disabled = true). 8. If the user inputs text in the input field, then the button will get enabled (disabled = false).
To disable the button we need to add a disabled attribute to the <button> element with a boolean value. if a boolean value is true button is disabled. if a boolean value is false button is enabled.
Use ngModel and bind a value and use to disable button. Try this:
<input type="text" autocomplete="off" [(ngModel)]="searchText"/>
<p-button (onClick)="saveNewField(searchText)" [disabled]="!searchText" label="Save"></p-button>
Typescript file:
export class AppComponent {
searchText: string;
saveNewField(searchText) {
console.log("searched Text", searchText);
}
}
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