Skip to content

Instantly share code, notes, and snippets.

@grekodev
Last active August 7, 2019 07:26
Show Gist options
  • Save grekodev/879f5bcb44eb04caf8c26be58e94132b to your computer and use it in GitHub Desktop.
Save grekodev/879f5bcb44eb04caf8c26be58e94132b to your computer and use it in GitHub Desktop.
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.0/knockout-min.js"></script>
<script> $(document).ready(() => {
ko.bindingHandlers.nextEnter = {
init: function (element, valueAccessor, allBindingsAccessor) {
$(element).on('keydown', 'input, select', function (e) {
var self = $(this), form = $(element), focusable, next;
if (e.keyCode == 13) {
focusable = form.find('input,a,select,button,textarea').filter(':visible');
var nextIndex = focusable.index(this) == focusable.length - 1 ? 0 : focusable.index(this) + 1;
next = focusable.eq(nextIndex);
next.focus();
return false;
}
});
}
};
ko.applyBindings({});
});</script>
//use
<div class="form-horizontal" data-bind="nextEnter:true">
// your_inputs
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment