I am binding an event to an element either using @click
property in Vue (it behaves the same as javascript .onclick
property).
The problem I have is when the event is called by propagating from the child DOM nodes click events I get the child DOM element as target
property and I cannot find a way a clean way (without searching for the parents, as the element might me nested deep inside) to access the DOM element that the event was registered onto inside the event callback.
JavaScript
x
5
1
<td @click="onCellClick($event)">
2
<div class="text">
3
<span>default</span>
4
</div>
5
</td>
Advertisement
Answer
I think the confusion is with the event’s target
vs currentTarget
event.target is what triggers the event dispatcher to trigger and event.currentTarget is what you assigned your listener to.
JavaScript
1
9
1
Vue.component('cell', {
2
template: '#cell',
3
methods: {
4
onCellClick: function (e) {
5
console.log(e.currentTarget)
6
}
7
}
8
});
9