Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  189] [ 5]  / answers: 1 / hits: 21285  / 9 Years ago, thu, april 2, 2015, 12:00:00

For each td element in a table I have an attached ng-click. Here is the (simplified) html for each table cell:



<td ng-click=cellClicked($event)>
<span ng-if=!cellEdit>{{event.eventName}}</span>
<input type=text ng-if=cellEdit ng-model=event.eventName>
</td>


And my (simplified) ng-click function:



scope.cellClicked = function (event) {
rowScope.cellEdit = true
angular.element(event.target).find('input').focus()
}


Its my goal to:




  1. User clicks a table cell

  2. Cell changes to edit mode

  3. Give focus to the input element located inside the td.



Right now this is working as long as the user clicks inside the td element but not on the span element:



console.log(angular.element(event.target)) #--> [td...] (as desired)


However if the user clicks on the span element within the td:



console.log(angular.element(event.target)) #--> [span...]


In this use case assigning focus does not work. I was hoping to access the parent element of the span doing something like:



angular.element(event.target.closest('td'))


or



angular.element(event.target.parentNode)


But it appears when an element gets passed through via $event and accessed there is no parent context.



How can I either:




  • Prevent clicking the span element firing the td's ng-click

  • On click of span element pass through it's html parent


More From » jquery

 Answers
14

Changing:



angular.element(event.target)


to:



angular.element(event.currentTarget)


fixed my issue.



It seems to me using event.currentTarget is preferred to event.target in the majority of usage cases.


[#67214] Tuesday, March 31, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skyler

Total Points: 646
Total Questions: 119
Total Answers: 96

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
;