here’s my code.
<input type="text" placeholder="Add New Tag" data-bind="value: tagToAdd, valueUpdate: 'afterkeydown', event: { keypress: addOnEnter }" />
here’s my knockout code.
self.addOnEnter = function (event) {
console.log(event.which);
var keyCode = (event.which ? event.which : event.keyCode);
if (keyCode === 13) {
self.addTag();
}
return true;
};
when I type something in the input field, then I log the event, kept getting undefined back. I dont know why I cannot catch which event is fired.
you can test my code on jsFiddle. http://jsfiddle.net/GBLNR/6/
just type anything in the input field, then watch the result from the console.
Knockout passes the current data context as the first argument to a handler. The
eventis the second argument.So, the signature for your function needs to be:
Alternatively, a good way to handle this is through a custom binding like:
Now you would just use this
enterKeybinding against youraddOnEnterfunction and it woul d only need to deal with your data.