Get parent id of current click element in AngularJS

How to get parent id of current clicked element in AngularJS?

<div id="d8" class="menutitles ng-scope" ng-repeat="title in list"> <div class="right" ng-click="showsubmenu($event)">+</div> <div class="left" ng-click="showsubmenu($event)">Unit 9</div> </div> 

How to get d8 value in showsubmenu($event) function?

Below I tried, but it does not work.

 $scope.showsubmenu=function(obj) { alert(obj.target.parent.attributes.id) } 
+6
source share
2 answers

It should be parentNode , not just parent :

 alert(obj.target.parentNode.id); 

Also attributes is redundant, as you can directly access the id property.

But note that since you have ngRepeat , it will create invalid markup as identifiers will be duplicated. You probably want to fix this too, maybe this is the case or use the classes:

 <div id="d8{{$index}}" class="menutitles ng-scope" ng-repeat="title in list"> <div class="right" ng-click="showsubmenu()">+</div> <div class="left" ng-click="showsubmenu()">Unit 9</div> </div> 
+14
source
 <div id="d8" class="menutitles ng-scope" ng-repeat="title in list"> <div class="right" ng-click="showsubmenu()">+</div> <div class="left" ng-click="showsubmenu()">Unit 9</div> </div> 

That should be enough: D

 function showsubmenu($event){ $($event.target).parent(); } 

A good day

+4
source

Source: https://habr.com/ru/post/975022/


All Articles