Colonel Mustard
Colonel Mustard

Reputation: 1533

jquery focus on next input of more than one type

I have the following method which works fine, it skips disabled fields and focus moves to the next active text input. My form has text fields, checkboxes and select lists. How do I add the type select to this so it navigates to type text or select?

I've tried [type=text:select] but it doesn't work

 $('input').on('keydown', function (e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        $('input[type=text]:enabled:not(:read-only)')[$('input[type=text]:enabled:not(:read-only)').index(this)+1].focus();
    }
});

Upvotes: 0

Views: 94

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388326

You can use multiple selector syntax, also cache the result of the element selection to avoid duplication

$('input, select').on('keydown', function (e) {
    if (e.keyCode == 13) {
        e.preventDefault();

        var $els = $('input:text, select').filter(':enabled:not([readonly])');
        $els.eq($els.index(this) + 1).focus();
    }
});

Upvotes: 3

Related Questions