Skip to content

A vanilla js way to trap the tab keypress inside an element.

Notifications You must be signed in to change notification settings

seanelliott86/a11y-trap-tabs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

a11y-trap-tabs

Vanilla JavaScript function to trap the tab keypress inside an designated element.

Example

https://codepen.io/seanus1138/full/ZjeaoJ/

How to use

Mark up your html how ever you would like, just make sure the target element that you will be passing to the trapTabKey() function wraps all the elements you want to trap focus to.

Review the code below:

HTML

<div class="js-target-elem">
    <ul>
	    <li><a href="#">Link 1</a></li>
	    <li><a href="#">Link 2</a></li>
	    <li><a href="#">Link 3</a></li>
	    <li><a href="#">Link 4</a></li>
    </ul>
    <p><a href="#">Another Link</a></p>
</div>

<p><a href="#">Outside target element</a></p>

Javascript

var elm = document.querySelector('.js-target-elem');

elm.addEventListener('keydown', function (e) {
    trapTabKey(elm);
}, false);

About

A vanilla js way to trap the tab keypress inside an element.

Resources

Stars

Watchers

Forks

Packages

No packages published