How to retrieve the clicked ElementId in angularjs?
Asked Answered
R

2

24

I have the following line:

<a href="#" id="12345" data-ng-click="ShowId()">

and in my controller I have:

$scope.ShowId = function(){
     alert('clicked element id in here: 12345');
};

How can I access in my controller ShowId function the id of the clicked element, in my case 12345?

Notice the bind is not within the ng-repeat so I can access the item id or something like that.

Reft answered 12/8, 2015 at 4:16 Comment(2)
Possible Duplicate: #24673918Cimabue
Why don't you simply pass data as a parameter? Data-ng-click="ShowId(1234)"Isoprene
R
35

I solved this:

<a href="#" id="12345" data-ng-click="ShowId($event)">   

$scope.ShowId = function(event)
{
   alert(event.target.id);
};
Reft answered 12/8, 2015 at 4:36 Comment(1)
it is not working for me, I have added id={{$index+1}} and same function I have writen in JS but event.target.id is showing blank in console. is there any other way ?Buenrostro
W
5
<button data-id="101" ng-click="showDetail($event)">click Me</button>

$scope.showDetail = function(event)
{
  console.log($(event.target).attr("data-id"));
}
Waterfall answered 6/2, 2016 at 12:37 Comment(1)
While your answer might be correct, without any explanation it's not especially helpful.Burrstone

© 2022 - 2024 — McMap. All rights reserved.