As per core we used this for get element but in AngularJS we get $scope context.
So, I tried this question but without success.
Here is my attempt:
Controller
$scope.clickMe = function(ele) {
console.log(ele);
console.log(ele.id);
};
HTML
<div class="input-group">
<input type="number" id="test-id" ng-blur="clickMe($event.target);">
</div>
How do I get the element in the function?
ggorlen
59.5k9 gold badges119 silver badges174 bronze badges
asked Jan 12, 2018 at 5:49
Jaydeep Mor
1,7233 gold badges22 silver badges42 bronze badges
2 Answers 2
use : $event.currentTarget;
<div class="input-group">
<input type="number" id="test-id" ng-blur="clickMe($event);">
</div>
$scope.clickMe = function(ele) {
console.log(ele);
console.log(ele.currentTarget);
};
answered Jan 12, 2018 at 6:05
Ved
12.1k5 gold badges46 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
John Rix
+1 for 'event.currentTarget'. I did not know about this, but it is great for getting the element that actually contains the click handler, as opposed to 'event.target' which may instead point to a child element that was actually clicked on (for example, a <span> element inside of a button that owns the ng-click handler).
your ng-blur event should send $event only like ng-blur="clickMe($event);"
then in your controller
$scope.clickMe = function(ele) {
console.log(angular.element(ele));
console.log(angular.element(ele).attr('id'));
};
Comments
default
thislikeng-blur="clickMe(this);", are you not getting element?$scopecontext.$event.currentTarget