Stfvns
Stfvns

Reputation: 1041

Send event target inside data-bind click html

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

Answers (1)

Muhammad Usman
Muhammad Usman

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

Related Questions