Oliver Watkins Oliver Watkins - 5 months ago 9x
Javascript Question

How to get the event source from calling element

I have an event listener in a list item :

<li class="item" ng-click="click($event)">
<a href="#/home">Home</a>

In my controller I have a click handler :

$scope.click = function($event) {
t2 = $($event.target.parentElement);

However, sometimes my $event.target is the list element, and sometimes it is the anchor element. This is because of where I click, sometimes it is on the a element but not on the list. Is there a way of getting from the event object the element that called the function?


Use $event.currentTarget

 $scope.click = function($event) {      
    t2 = $($event.currentTarget.parentElement);