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