Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular click event on component send inner element as target

I have a click event on an Angular component:

import { Component } from "@angular/core";

@Component({
  template: `
    <hello (click)="onClick($event)"></hello>
  `
})
export class AppComponent {
  onClick(e) {
    alert("Click on TAG: " + e.target.tagName);
  }
}

See demo online: https://stackblitz.com/edit/angular-ivy-zxmsnc?file=src%2Fapp%2Fapp.component.ts

Why is event.target an inner element of the component and not the component itself?

like image 508
ar099968 Avatar asked Oct 18 '25 03:10

ar099968


1 Answers

That's because event.target refers to the element that triggered the event.

I think you're looking for event.currentTarget which refers to the element that the event listener is attached to.

like image 123
yurzui Avatar answered Oct 20 '25 17:10

yurzui