2

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?

midstack
  • 2,105
  • 7
  • 44
  • 73
  • [this](https://stackoverflow.com/questions/30442576/parents-in-angular) might help – Towkir Nov 06 '18 at 07:07
  • right off the bat. I'd suggest assigning values to arbitrarily create priority of children or giving them some type of weight such as an id and then target that instead. – Rogelio Nov 06 '18 at 07:11

1 Answers1

1

$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;
Amrit
  • 2,115
  • 1
  • 21
  • 41