Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enble/disable a button in TypeScript 1.5?

People also ask

How do I set the Disable 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 know if a button is disabled in TypeScript?

First, you use Document. querySelector() (Or document. getElementById(), getElementsByTagName(), etc) to retrieve the button Element (lets assume it has a class of buttonA ). Next, you can simply access its attributes and check its values.

How do you disable a button element?

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.


There should be a cleaner way to do this:

var element = <HTMLInputElement> document.getElementById("btnExcel");
element.disabled = true;

Or if you prefer a one liner:

(<HTMLInputElement> document.getElementById("btnExcel")).disabled = true;

It seems getElementById and the like should be a generic method and accept a type parameter. That said, a generic method wouldn't give you anything that typecasting doesn't.


I'm using TypeScript v 3.7.something. In here

(document.getElementById('button') as HTMLInputElement).disabled = false;

is prefered. (guided by @Martin's answer)


I'm using "typescript": "~3.8.3" in Angular 9. To disable a button successfully try:

let button = <HTMLButtonElement> document.getElementById('confirmBtn');
button.disabled = true;

Problem solved by using <HTMLInputElement>
Just try this..

var previous = <HTMLInputElement>document.getElementById('previous');

previous.disabled = true;