FastTrack
FastTrack

Reputation: 8980

Find Max tabindex Within Form

Using jQuery/Javascript, how do I find the maximum tabindex value that exists on a given form #formID?

And before you ask (because I know people will) I haven't tried anything because I really have no idea where to begin.

Upvotes: 5

Views: 3481

Answers (2)

SZL
SZL

Reputation: 855

With Javascript:

var maxTabIndex = -1;

document.getElementById("formID").querySelectorAll("input").forEach((e) => {
    if (!isNaN(e.tabIndex)) {
        if (e.tabIndex > maxTabIndex) {
            maxTabIndex = e.tabIndex;
        }
    }
});

Upvotes: 0

Teemu
Teemu

Reputation: 23396

This is one way:

var max = -1;
$('#formID [tabindex]').attr('tabindex', function (a, b) {
    max = Math.max(max, +b);
});

max = -1 indicates, that there's no tabindexes in the form, or elements are excluded from tabbing order.

A live demo at jsFiddle.

Upvotes: 11

Related Questions