Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How disable/enable button with ionic 2?

I have an input field and a button. It must be disable at start. When the input no blank is, the button is enable.

I use a ngModel to take the value of the input and a function (change) to start a function each time the input is changed.

Now I do a little if in the change function.

if(input !== ''){
//enable the button
}else{
//disable the button
}

Have you any idea how to achieve that?

Thanks

like image 902
anubis Avatar asked Feb 03 '17 13:02

anubis


People also ask

How do I disable the Enable button?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

How do I disable the button if the input box is empty and enable when field is filled?

How do you disable button until all fields are entered? Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do I turn off PrimeNG button?

It is of the boolean datatype, the default value is true. Angular PrimeNG Focus Trap Disabled Button properties: disabled: It specifies the button to be disabled if the value is set to true. The default value is false.


1 Answers

Just have a boolean variable in class:

isenabled:boolean=false;

Change function

if(input !== ''){
//enable the button
isenabled=true; 
}else{
//disable the button
isenabled=false;
}

In Html:

<button ion-button [disabled]="!isenabled"></button>

For changing classes:

<button ion-button [ngClass]="{class:isenabled,class2:!isenabled}"></button>

Check here

like image 142
Suraj Rao Avatar answered Sep 21 '22 13:09

Suraj Rao