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.

Revisions

  1. grekodev revised this gist Aug 7, 2019. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion nextEnter.js
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,9 @@
    }
    };
    ko.applyBindings({});
    });</script>
    });</script>

    //use
    <div class="form-horizontal" data-bind="nextEnter:true">
    // your_inputs
    </div>
  2. grekodev created this gist Aug 7, 2019.
    18 changes: 18 additions & 0 deletions nextEnter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <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>