Oliver Watkins Oliver Watkins - 2 years ago 81
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?

Answer Source

Use $event.currentTarget

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download