Kode_12 Kode_12 - 2 months ago 14
AngularJS Question

How can I pass in an element's ID on an ng-hover event?

I have an element like so:

<div id="one" ng-mouseover="showDefinition()"> Some Text</div>
<div id="one" ng-mouseover="showDefinition()"> Some Text</div>
<div id="one" ng-mouseover="showDefinition()"> Some Text</div>


When I hover over each respective element, it fires the showDefinition function:

scope.showDefinition = function showDefinition() {
console.log("youve hovered over the id : + <element's id>");
}


How can I pass in the element id in which the hover was triggered?

Answer
scope.showDefinition = function showDefinition(e) {
  console.log("youve hovered over the id : + e.target.id");
}

and in the HTML

<div id="one" ng-mouseover="showDefinition($event)"> Some Text</div>

Plunkr: https://plnkr.co/edit/CW9kXLzc23xcWlA5EJzm?p=preview