Vincent Dagpin
Vincent Dagpin

Reputation: 3611

Get attribute value from ng-click $event

I have an anchor that has a bold text and a ng-click event for angular. In controller, i want to get the property href but i got a problem. It will work if i will not put a bold tag inside anchor since the event.target will target directory to anchor but if i put a bold tag on the text, it (event.target) will not target to anchor but will target to bold tag

I tried this but still the result is undefined

<div ng-app="miniapp">
    <div ng-controller="Ctrl">
        <a  ng-click="clickEvent($event)" 
            href="http://google.com"><b>Click Me</b></a>
    </div>
</div>

var app = angular.module('miniapp', []);

function Ctrl($scope) {
    $scope.clickEvent = function(obj) {
        obj.preventDefault();        

        var target = angular.element(obj.target);
        alert(target.attr('href'));
    }
};

Click Here for fiddle

Upvotes: 0

Views: 4369

Answers (1)

tasseKATT
tasseKATT

Reputation: 38490

Instead of:

obj.target

Use:

obj.currentTarget

Current target is the element the event listener is created on, target is the element that triggered the event.

Upvotes: 1

Related Questions