Skip to content Skip to sidebar Skip to footer

How To Get Dom Element With Ng-click

I have some element like : And function in controller : $scope.weirdFunction = function(element) { console.l

Solution 1:

Please try following snippet.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.weirdFunction = function(element) {
     console.log(element);
    }  
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="myApp"ng-controller="myCtrl"><buttonng-click="weirdFunction($event.currentTarget)">Pretty Button</button></div>

Solution 2:

Pass $event object to weirdFunction. $event.target gives the element on which the click was triggered.

<button ng-click="weirdFunction($event)">Pretty Button</button>

Solution 3:

I had this question now and concludes by creating this function

getElement($event){
    return$event.target || $event.srcElement || $event.toElement || $event.path[0];
};

At CanIUse it works everywhere.

Post a Comment for "How To Get Dom Element With Ng-click"