Hi,
I have a button like below
How to call the id - btn from component to typescript file to work for enable or disable
Thanks
Hi,
I have a button like below
How to call the id - btn from component to typescript file to work for enable or disable
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 12:51 AM EDT
For the code below:
function test() { const btn = document.getElementById("btn") as HTMLButtonElement; // Disable the button btn.disabled = true; // Enable the button btn.disabled = false; }there are a few style and best practice improvements that could be made:
1) It's generally a good practice to use a more descriptive name for the function, such as "toggleButtonState" or "enableDisableButton".
2) Since we know the type of the element we are accessing, we can use the more specific HTMLButtonElement type instead of casting to an HTMLButtonElement using the "as" keyword.
3) It's a good practice to cache the result of getElementById to avoid searching the DOM tree multiple times for the same element.
4) If we're just toggling the button state, we can use the "toggle" method instead of setting the "disabled" property directly.
function toggleButtonState() { const btn = document.querySelector("#btn") as HTMLButtonElement; // Disable the button if it's currently enabled, or vice versa btn.toggleAttribute("disabled"); }we use the querySelector method to select the button element by its ID, and use the toggleAttribute method to toggle the "disabled" attribute of the button.
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 15, 2023, 11:51 PM EDT
Hello Sathish,
Try below code:
function test() { const btn = document.getElementById("btn") as HTMLButtonElement; // Disable the button btn.disabled = true; // Enable the button btn.disabled = false; }Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 11:23 AM EDT
To access the
btnbutton from the component's TypeScript file, you can use Angular'sViewChilddecorator to get a reference to the button element.Vishal YelveRank #178 · 13.6K reputationPosted Mar 15, 2023, 2:52 PM EDT
Please refer below link
https://bobbyhadz.com/blog/typescript-disable-button
https://tutorialscamp.com/angular-disable-button/
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 12:34 AM EDT
In your component class, import the ViewChild decorator from @angular/core and use it to get a reference to the button element:
import { Component, ViewChild } from '@angular/core'; @Component({ selector: 'app-my-component', template: ` To Test `, }) export class MyComponent { @ViewChild('btn') btnElement: any; test() { // enable or disable the button this.btnElement.disabled = !this.btnElement.disabled; } }we use @ViewChild to get a reference to the button element using its template reference variable "#btn". We then store the reference to the button element in a class property called "btnElement".
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 16, 2023, 1:14 AM EDT
Hi satish,
Please refer this link which will help you,
https://stackblitz.com/edit/angular-7pdxqk?file=src%2Fapp%2Fapp.component.html