Submit form on “Enter”

Tags: html, form, enter, angularjs, directive

If you are using bootstrap and Angularjs you could use a directive:


<form osi-form> 
...
<input type='text'>
...
<button type='button'>Login</button>
</form>

Here's the directive code:


(function () {
// submit form on enter. for example login.
// mark the default button as .btn.btn-primary
angular.module('app')
    .directive('osiForm', function () {
        return {
            restrict: 'A',
            link: function (scope, element) {
                var container = element;
                $('input', container).on('keydown', function (e) {
                    if (e.keyCode == 13) {
                        $('.btn.btn-primary', container).click();
                    }
                });
            }
        }
    });

})(window.angular);

No Comments

Add a Comment