1

I have this code:

<i class="fa fa-eye" (click)="passclass($event)"></i>

I know that I can get the id of the element in the $event.

I was wondering is I can get the element's class too?

4
  • 1
    Can you give more details about the element class ? is it an element of an array ( as *ngFor) ? Commented Jul 29, 2021 at 12:11
  • It's just this ... class="fa fa-eye" pass: "fa fa-eye" so I can add some logic into it...for example: if ($event.theClass === 'fa-eye') { //do something } Commented Jul 29, 2021 at 12:16
  • you can edit the function passClass in order to receive as a param the class name. <i class="fa fa-eye" (click)="passclass($event, 'fa-eye')"></i> Commented Jul 29, 2021 at 12:21
  • I can't pass it like that as this class contents are going to change so I need to get it by code Commented Jul 29, 2021 at 12:23

2 Answers 2

3

you can get a class name like below code

  if(event.target?.className === 'fa fa-eye'){
                //do something
            }
Sign up to request clarification or add additional context in comments.

Comments

2

Update : Sorry, the solution I proposed works in Javascript but not properly in Angular

In fact you could instead use template variable like that :

<i #element class="fa fa-eye" (click)="passclass($event, element.classList)"></i>

1 Comment

I'm getting: Property 'classList' does not exist

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.