I want to disable button if input length is less than 3:
<input type="text" [(ngModel)]="newListItem">
<button [disabled]="{ disabled : newListItem.length < 3 }"></button>
But this code doesn't work. How can I achieve my goal?
To conditionally disable a button element in Vue. js, you can dynamically bind the disable attribute to an expression that evaluates to boolean true (to disable the button) or false (to enable the button).
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.).
You can disable the <button> element in HTML by adding the disabled attribute to the element. The disabled attribute is a boolean attribute that allows you to disable an element, making the element unusable from the browser.
You only need to pass an expression to disabled
property, there's no need to create object:
<button [disabled]="newListItem.length < 3"></button>
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