javascript get element with focus code example
Example 1: Javascript find element with focus
var currentElementWithFocus=document.activeElement;
Example 2: javascript trap focus
function _focusTrapperEvent(e) {
if (!(e.key === 'Tab' || e.keyCode === 9))
return;
let condition = 'a[href], area[href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object:not([disabled]), embed, *[tabindex], *[contenteditable]',
first = $('.focusKeeper').find(condition).filter(':not(.select-element):visible:not(.invisible):first'),
last = $('.focusKeeper').find(condition).filter(':not(.select-element):visible:not(.invisible):last');
console.log(first);
console.log(last);
if ( e.shiftKey ) {
if (document.activeElement === first[0]) {
last[0].focus();
e.preventDefault();
}
} else {
if (document.activeElement === last[0]) {
first[0].focus();
e.preventDefault();
}
}
}