Scipion
Scipion

Reputation: 11888

Angular2 Get element where an event is fired from

Here is my code :

<div id="foo" (click)="$event.target.classList.toggle('selected')">
   <div>...</div>
   <div>...</div>
   <div>...</div>
</div>

I would like to toggle the class 'selected' on the div#foo thus $event.target isn't correct since clicking one of the inside divs will return that div and not div#foo. Any idea how to always target div#foo when clicking on one of the element inside ?

Upvotes: 0

Views: 253

Answers (1)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37413

use the currentTarget instead of target

<div id="foo" (click)="$event.currentTarget.classList.toggle('selected')">
   <div>...</div>
   <div>...</div>
   <div>...</div>
</div>

Upvotes: 1

Related Questions