Skip to content

Instantly share code, notes, and snippets.

View rajeshbosak's full-sized avatar
🏠
Working from home

Rajesh rajeshbosak

🏠
Working from home
View GitHub Profile
@rajeshbosak
rajeshbosak / trap_focus.js
Created August 24, 2020 09:37 — forked from myogeshchavan97/trap_focus.js
Code for trapping focus inside modal
// add all the elements inside modal which you want to make focusable
const focusableElements =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const modal = document.querySelector('#exampleModal'); // select the modal by it's id
const firstFocusableElement = modal.querySelectorAll(focusableElements)[0]; // get first element to be focused inside modal
const focusableContent = modal.querySelectorAll(focusableElements);
const lastFocusableElement = focusableContent[focusableContent.length - 1]; // get last element to be focused inside modal