How to call a function using knockout.js when enter key is pressed.. here is my code below.
ko.bindingHandlers.enterkey = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { var inputSelector = 'input,textarea,select'; $(document).on('keypress', inputSelector, function (e) { var allBindings = allBindingsAccessor(); $(element).on('keypress', 'input, textarea, select', function (e) { var keyCode = e.which || e.keyCode; if (keyCode !== 13) { alert('a'); return true; } var target = e.target; target.blur(); allBindings.enterkey.call(viewModel, viewModel, target, element); alert('b'); return false; }); }); } }; ko.applyBindings(viewModel);
HTML
<input type="text" data-bind="value:sendMessageText, enterkey: sendMessage" />
ViewModel
function contactsModel(){ var self = this; self.jid=ko.observable(); self.userName=ko.observable(); self.sendMsgText=ko.observable(); self.sendMessage = function(){ if(self.sendMessageText() == '' ) alert("Enter something in input field"); else{ var message = { to : self.userName(), message : self.sendMsgText() } self.sentMessages.push(message); sendMessage(message); } } }
Any idea's about what is wrong here or suggestions for better approach.
You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.
To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.
JavaScript Learn JavaScript Quick Course Beginners Use the ENTER's keycode 13.
No need for a custom binding, just use knockout's keypress event(Knockout docs):
<input type="text" data-bind="textInput : keyword, event: {keypress: onEnter}" > </input>
And your function:
that.onEnter = function(d,e){ e.keyCode === 13 && that.search(); return true; };
JSFiddle example
EDIT: New binding from knockout(3.2.0) : textInput - obviates the need to have a valueUpdate binding.
When you create your own knockout bindingHandler, it is used in the same way as the other bindingHanlders eg: data-bind="text: myvalue"
so your HTML will need to look something like this
<input type="text" data-bind="value:sendMessageText, valueUpdate: 'afterkeydown', enterkey: sendMessage" />
An important binding to add is the valueUpdate: 'afterkeydown'
binding. Without this binding when a user types text in the input and hits enter the onblur event is not raised prior to enterkey
binding. This results in the observable returning an unexpected value and not the current text if the input's value is accessed in an action invoked by enterKey
.
Another Look at Custom Bindings for KnockoutJS
EDIT
This is what I have used previously on other projects. JsFiddle Demo
ko.bindingHandlers.enterkey = { init: function (element, valueAccessor, allBindings, viewModel) { var callback = valueAccessor(); $(element).keypress(function (event) { var keyCode = (event.which ? event.which : event.keyCode); if (keyCode === 13) { callback.call(viewModel); return false; } return true; }); } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With