user2881493
user2881493

Reputation: 47

Set Html-Tabindex back to "auto"

On a Page with many form-fields I wanna set specific ones to tabindex="-1". But when the user checks a checkbox the fields with tabindex="-1" should get reached by a tabular as usual. How to do this? Tabindex="auto" & tabindex="" don't work.

My Javascript:

function setTabindex(checkboxChecked) {
    var tabindexValue = cbChecked ? '' /* <-- what should I enter here? */ : '-1';
    var noTabindexElements = document.querySelectorAll('input[data-notabindex]');

    for (var i = 0; i < noTabindexElements.length; i++) {
        noTabindexElements[i].setAttribute('tabindex', tabindexValue);
    }
}

Upvotes: 3

Views: 4330

Answers (2)

Mateusz Zębala
Mateusz Zębala

Reputation: 1

For all those who are looking for something better, you can also do this:

input.setAttribute("tabindex", null);

Upvotes: 0

Korikulum
Korikulum

Reputation: 2599

According to MDN & HTML specs this should work, but I haven't tested it:

function setTabindex(checkboxChecked) {
    var tabindexValue = cbChecked ? 0 : -1;
    var noTabindexElements = document.querySelectorAll('input[data-notabindex]');

    for (var i = 0; i < noTabindexElements.length; i++) {
        noTabindexElements[i].tabIndex = tabindexValue;
    }
}

Upvotes: 2

Related Questions