4

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?

3
  • when you pass this like ng-blur="clickMe(this);", are you not getting element? Commented Jan 12, 2018 at 5:54
  • @Ved Nop.... it return $scope context. Commented Jan 12, 2018 at 5:56
  • Okay. than you can use $event.currentTarget Commented Jan 12, 2018 at 6:03

2 Answers 2

8

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);
    };

Fiddle" http://jsfiddle.net/h8to34ux/221/

Sign up to request clarification or add additional context in comments.

1 Comment

+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).
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.