Reputation: 1041
I can't send event target when send parameter in data-bind.
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>
self.notify = function (str, id, e) {
if (!e) e = window.event;
console.log(str + id);
console.log(e.currentTarget);
});
The result of currectTarget
is undefined
. But when not using parameter this will working. example working:
<button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
self.notify = function (data, e) {
console.log(e.currentTarget);
});
The output will be <button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
How to get event target when I'm parsing multiple parameter
Upvotes: 2
Views: 147
Reputation: 10148
In Knockoutjs
, event
is always passed as the last argument to the event handler. So you can catch it there and use it like
function vm(){
this.notify = function (str, id, e,event) {
console.log(event.currentTarget)
}
}
ko.applyBindings(new vm())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>
Upvotes: 1