Reputation: 7197
I am using angularjs. My structure is like this (inside larger table - just showing one cell):
<td>
<div>
<label>OU5NS1E2IE1L</label>
<i class="fa fa-times" aria-hidden="true"
ng-click="DeleteElementFromSession($event);"></i>
</div>
</td>
When calling DeleteElementFromSession
this line of code:
$(event.currentTarget).parent()
returns DIV
, which is correct. But I want to return <td>
element, where DIV
is inside. I don't know why, call to this doesn't return <td>
element? It doesn't return anything:
$(event.currentTarget).parent().parent()
<i>
is inside DIV
and DIV
is inside td
so in my opinion, this should work, but it doesn't. Why?
Upvotes: 0
Views: 102
Reputation: 48948
The browser is eliminating the <td>
node from the DOM because it is not legal HTML.
The <td>
node needs to be a child of node of <table>
and <tr>
.
<table>
<tr>
<td id="tdNode1">
<div>
<label>OU5NS1E2IE1L</label>
<i class="fa fa-times" aria-hidden="true"
ng-click="DeleteElementFromSession($event);">CLICK HERE</i>
</div>
</td>
</tr>
</table>
Then the browser will include the <td>
node in the DOM and elem.parent().parent()
will find the <td>
node.
app.controller("myVm", function($scope) {
var vm=$scope;
vm.DeleteElementFromSession = function(e) {
var elem = angular.element(e.currentTarget);
vm.id = elem.parent().parent().attr("id");
}
});
The DEMO on JSFiddle.
Upvotes: 1
Reputation: 881
for this you need parent with closest:
$(event.currentTarget).parent().closest('td')
Upvotes: 0
Reputation: 1223
According to Angular JS doc:
https://docs.angularjs.org/api/ng/function/angular.element
parent() - Does not support selectors
parent is from jqLite which contains some of the jQuery functions.
According to jQuery doc:
https://api.jquery.com/parent/
This method is similar to .parents(), except .parent() only travels a single level up the DOM tree.
so only one level up travels is allowed.
Upvotes: 1