Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Class Name of Element from MouseEvent Target

I need the class name of an EventTarget, which I'm getting like this:

<div class="orig-post" (mousemove)="onMouseMove($event)"></div>

onMouseMove(event: MouseEvent): void {
    this.target = event.target
    console.log(this.target) // Outputs the element the mouse is currently over
}

I'd like to be able to assign a variable to the class name of the element that is the target. If I were using something like getSelection() I could just do something like selection.anchorNode.parentElement.className. Is there a way to do this in pure javascript? I'm using Angular 2 and would like to avoid jQuery entirely.

like image 398
J. Adam Connor Avatar asked Apr 26 '17 15:04

J. Adam Connor


1 Answers

In order to silence the typescript warning you can cast EventTarget to Element and then use className property

(event.target as Element).className
like image 91
yurzui Avatar answered Nov 12 '22 01:11

yurzui