user2203384 user2203384 - 1 year ago 90
jQuery Question

Jquery find next input, select on keypress but not button

I want to select the next input:visible and select:visible when the user press "enter". My code work except it finds the next

element and I don't want that.

$(document).on('keypress', 'input, select', function (e) {
if (e.which == 13) {
var inputs = $(this).closest('form').find(':input:visible, select:visible');
inputs.eq( inputs.index(this)+ 1 ).focus();

How to filter the button ?


Answer Source

You could use :not to exclude some elements:

var inputs = $(this).closest('form').find(':input:not(button,[type="button"],[type="submit"]):visible, select:visible');

This gets quite verbose though. A better solution may be to put a common class on the input and select elements you do want to find and select that, eg .foo:visible