Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

html this.id to function not working angular

I’m trying to send the id of a element from html to the function, this because it changes in the ts, so I have a click function but function(this.id) doesnt work
here is my code

the html

<button id="btnNext" class="btn" (click)="nextGeneral(this.id)">next</button>

the ts

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

nextGeneral(id:number){
alert(id)
}

enter image description here

>Solution :

If I understand correctly, you want the id attribute from the <button> (i.e. btnNext) to be passed into the nextGeneral() function (although this function is currently expecting a number not a string, so correct me if I’m wrong).

This is how you could achieve that dynamically:

.html

<button id="btnNext" class="btn" (click)="nextGeneral($event)">next</button>

.ts

nextGeneral(event: PointerEvent){
  const id: string = (event.target as HTMLElement).id
  alert(id)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading