4

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?

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.