Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find next element without attribute

I have a large form that I want to navigate through by pressing enter instead of tab. I have a simple script which worked fine until I added some disabled fields into the mix. How can I skip the fields that have the attribute of disabled?

I've tried using a loop but I cannot get it to skip the two disabled fields together and focus to the next one, as it seems to stay on the field before the disabled ones.

Failing that, is there a way to replace the enter keyCode of 13 with the one for tab? I've tried a few solutions here but none of them seem to work

$('input').on('keydown', function (e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        $('input')[$('input').index(this)+1].focus();
    }
});
like image 260
Colonel Mustard Avatar asked Apr 15 '26 22:04

Colonel Mustard


1 Answers

You can use the :enabled selector.

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

JSFiddle

like image 178
Schleis Avatar answered Apr 17 '26 12:04

Schleis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!