I've been working on implementing an accessibility feature for a bootstrap accordion. I included the tabindex=0
in each <a>
tag in an attempt to make it so that when the user presses the tab key, it opens the corresponding dropdown/accordion. However, my current code is opening every accordion option when the tab button is pressed. Is there a way to use a forEach
on keydown?
$('.accordion-toggle').keydown(function(e) {
if (e.keyCode == 13) {
$('.collapse').addClass('show');
} else {
$('.collapse').removeClass('show')
}
});
<a
class="text-left w-100 btn btn-link accordion-toggle"
tabindex="0"
data-toggle="collapse"
data-target="#collapseEight"
aria-expanded="true"
aria-controls="collapseEight">
item 1 <i class="arrow down"></i>
</a>
<div id="collapseSeven" class="collapse" aria-labelledby="headingSeven" data-parent="#accordion">
<p> Content for this section</p>
</div>
<a
class="text-left w-100 btn btn-link accordion-toggle"
tabindex="0"
data-toggle="collapse"
data-target="#collapseEight"
aria-expanded="true"
aria-controls="collapseEight">
item 2 <i class="arrow down"></i>
</a>
<div id="collapseSeven" class="collapse" aria-labelledby="headingSeven" data-parent="#accordion">
<p> Content for this section</p>
</div>