showing results for - "javascript trap focus"
Kevin
29 Jan 2016
1/**
2*Function should be called on keyPress or something
3*/
4function _focusTrapperEvent(e) {
5  if (!(e.key === 'Tab' || e.keyCode === 9))
6    return;
7  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]',
8      first = $('.focusKeeper').find(condition).filter(':not(.select-element):visible:not(.invisible):first'),
9      last = $('.focusKeeper').find(condition).filter(':not(.select-element):visible:not(.invisible):last');
10
11  console.log(first);
12  console.log(last);
13
14  if ( e.shiftKey ) /* shift + tab */ {
15    if (document.activeElement === first[0]) {
16      last[0].focus();
17      e.preventDefault();
18    }
19  } else /* tab */ {
20    if (document.activeElement === last[0]) {
21      first[0].focus();
22      e.preventDefault();
23    }
24  }
25}