4

I want access to parent element from ng-click event's target element and remove it.

I looked some pages and angular docs and found something like below but this is not worked for me.

My Template:

<div class="element-which-i-want-access">
   <span>
      <button ng-click="remove(myModelObjectInCurrentScope, $event)" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></button>
   </span>
</div>

My Controller:

$scope.remove = function(object, $event) {
   var el = $event.target; // this meaning as clicked <button> element 
   var myTargetElement = el.parent().parent(); // this not working
   myTargetElement.remove(); // i couldn't tried this step but i couldn't got parent element yet
}

How can I do this? Thanks in advance.

0

2 Answers 2

4

$event.target will give the DOM element. To use parent() on it, it need to be wrapped as follow

angular.element(el).parent().parent();
Sign up to request clarification or add additional context in comments.

Comments

0

accepted answer did'nt work for me. but this code did:

var elem= angular.element($event.currentTarget);
var parent = elem.parent().parent();
parent.remove();

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.