midstack
midstack

Reputation: 2123

How can I check closest parent classname in angular 6?

I want to check classname which the closest parent of the clicked element in angular 6?

HTML

<div class="parent-element selected" (click)="checkClass($event)">
    <ul>
         <li><a>Link-1</a></li>
         <li><a>Link-2</a></li>
         <li><a>Link-3</a></li>
    </ul>
</div>

ANGULAR

checkClass(element) {
 return element.target.classList.contains('selected');
}

if I check the classname by using "checkClass" function, it doesn't always give the right result, because there is a possibility of clicked element other than a parent. So I want to first find the closest parent of the clicked element and than check the classname. How can I do that?

Upvotes: 2

Views: 2591

Answers (1)

Amrit
Amrit

Reputation: 2165

$event.target.parentNode; will give you parent element.

you can get parent element by this:

 $event.target.parentElement

and all the class list by this:

 $event.target.parentElement.classList;

Upvotes: 1

Related Questions