Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active March 31, 2017 07:52
Show Gist options
  • Save pokk/93705177bc43e41256a2284f7cd37dee to your computer and use it in GitHub Desktop.
Save pokk/93705177bc43e41256a2284f7cd37dee to your computer and use it in GitHub Desktop.
Automatically jump to next item

Intrduction

When we click enter key, when can foucs to next item automatically.

Javascript

$('body').on('keydown', 'input, select, textarea', function (e)
{
    var self = $(this),
        form = self.parents('form:eq(0)'),
        focusable,
        next;
    // "ENTER" key code is 13.
    if (e.keyCode == 13)
    {
        focusable = form.find('input, a, button, select, textarea').filter(':visible');
        next = focusable.eq(focusable.index(this) + 1);
        if (next.length)
        {
            next.focus();
        }
        else
        {
            next.submit();
        }

        return false;
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment