Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable the button if the input is empty

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>
like image 914
E.Meir Avatar asked Jun 27 '18 13:06

E.Meir


People also ask

How do I disable input submit 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.

How do I make a button disabled?

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.).

How do you disable a button if the input field is empty?

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).

How do you disable button if input is empty in React in functional component?

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.


1 Answers

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);
  }
}
like image 86
Pradnya Sinalkar Avatar answered Sep 19 '22 02:09

Pradnya Sinalkar