0

Let say I have the following in angular:

<div class="dwiws" id="som" (click)="someFunction()">
      some content                                
</div>

Then in .ts, I have the following:

  someFunction(){
       //Want to detect the class and id of the div i clicked.
  };

I want to detect the id and class of the div that the click function lies.

How do I do it?

Steve Kim
  • 5,293
  • 16
  • 54
  • 99

1 Answers1

1

You can use srcElement property and get that info.

<button (click)="someFunction($event)" id="myid" 
     class="myclass">Click</button>


  someFunction(event) {
      console.log(event);
      console.log(event.srcElement.attributes.id);
      console.log(event.srcElement.attributes.class);
  }
Mani S
  • 2,501
  • 1
  • 12
  • 12