9

I am getting this JS error: ReferenceError: event is not defined when I try to pass the event object to click binding when I use Firefox 23. Everything works fine under Chrome

Here the code:

<!-- ko foreach: entries -->
   <tr data-bind="click: function(){ $parent.expandRow($data, event) }">
      ...
   </tr>
<!-- /ko -->


vm.entries.expandRow = function(entry, event){
    ...           
}
Marco C
  • 3,101
  • 3
  • 30
  • 49
  • Just found this http://stackoverflow.com/questions/2974601/event-is-not-defined-in-firefox-but-ok-in-chrome-and-ie probably it is the cause – Marco C Aug 14 '13 at 00:57

2 Answers2

15

Here the solution from github.com/knockout/knockout/issues/752

<!-- ko foreach: entries -->
   <tr data-bind="click: function(data, event){ $parent.expandRow($data, event) }">
      ...
   </tr>
<!-- /ko -->

Under Firefox event is not defined on the window object, instead it needs to be passed to the function.

Marco C
  • 3,101
  • 3
  • 30
  • 49
  • In my case passing the event object in the function did not work. I achieved it with `data-bind="click : function(){ $root.clicked(event)}"`. Of course, the knockout version I used is different. It is 3.4.2 – Vinit Divekar Jul 25 '18 at 04:11
0

I know that this is a pretty old question, but still if someone is looking for the answer then I achieved it in the following way:

<div id="this-element" data-bind="click : function(){ $root.clicked(event)}">

</div>

Following code (clicked function) is written in the ViewModel for the page:

this.clicked= function (event) {
    console.log(event.currentTarget.id);
}

Knockout 3.4.2

jQuery 3.2.1

Vinit Divekar
  • 774
  • 10
  • 24