Can I dynamically set tabindex in JavaScript?

JavascriptHtmlAccessibility

Javascript Problem Overview


Is there any attribute like tab-index?

CONTEXT : I'm making a section in a web form visible or invisible depending on some condition where I want to set the tab-index manually when that particular section is visible.

Javascript Solutions


Solution 1 - Javascript

document.getElementById("link3").tabIndex = 6;

Solution 2 - Javascript

Using JQuery we can set tab index dynamically easily Try this code- set the tabindex and increment the variable

$(function() {
    var tabindex = 1;
    $('input,select').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });
});

Solution 3 - Javascript

Dynamically setting tabindex = "-1" for readonly inputs

That is an interesting question; the more that CSS support is still not available.

Here is how tabindex can be set to -1 for all readonly input elements:

NodeList.prototype.forEach = NodeList.prototype.forEach || Array.prototype.forEach;

document.querySelectorAll('input[readonly="readonly"]').forEach(x => x.tabIndex = -1);

The first line ensures that the NodeList class is extended with the forEach method. This is further explained here.

Solution 4 - Javascript

Dynamically create and reset tabIndex of an HTML elements.

The tabindex attribute specifies the tab order of an HTML element, such as set of "li","a" e.t.c. The tabindex attribute is supported in all major browsers.

For this instance let set tabindex for list items "li". Usually tabindex will start from '0', however we can reset it to start from '1'. I am using Jquery to do this.

See It Working Here

<ul id="dfruits">
<li>Apple</li>
<li>Dragonfruit</li>
<li>Damson</li>
<li>Cloudberry</li>
<li>Blueberry</li>
<li>Cherry</li>
<li>Blackcurrant</li> 
<li>Coconut</li>
<li>Avocado</li>   
 <li>Pinaple</li>     
</ul>

$(document).ready(function() {

var 
SomeFruitsList=$("ul#dfruits li"),
//set tab index to starts from 1
tabindex = 0;	
    
SomeFruitsList.each(function() {
 // add tab index number to each list items
  tabindex++; 
$(this).attr("tabindex","TabIndex  " +tabindex); 

var tabIndex = $(this).attr("tabindex");
 // add tab index number to each list items as their title   
$(this).attr("title",tabIndex);
    
    $(this).append('<br/><em>My tabIndex is number:    '+tabIndex+'<em>')
})
    });

Solution 5 - Javascript

Some useful JS:

for (let tabbable of document.querySelectorAll('[tabindex]')) {

}

element.setAttribute('tabindex', '-1');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRVKView Question on Stackoverflow
Solution 1 - JavascripthunterView Answer on Stackoverflow
Solution 2 - JavascriptNikzView Answer on Stackoverflow
Solution 3 - JavascriptSerge StroobandtView Answer on Stackoverflow
Solution 4 - JavascriptShapCyberView Answer on Stackoverflow
Solution 5 - JavascriptAndrewView Answer on Stackoverflow