Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function on enter key press

Tags:

knockout.js

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.

like image 297
Dave Avatar asked Apr 15 '14 15:04

Dave


People also ask

How do you call a function on Enter key press?

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.

How do you detect enter press?

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.

What is the key for enter in JavaScript?

JavaScript Learn JavaScript Quick Course Beginners Use the ENTER's keycode 13.


2 Answers

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.

like image 141
DaafVader Avatar answered Oct 22 '22 04:10

DaafVader


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;         });     } }; 
like image 45
Nathan Fisher Avatar answered Oct 22 '22 05:10

Nathan Fisher