diff --git a/index.js b/index.js index 8f6169e..16df951 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ csSelector.addEventListener('keyup', function(e) { }) document.addEventListener('click', function(e) { - if (!e.target.closest('#myCustomSelect')) { + if (!aParentElementHasId(e, 'myCustomSelect', 20)) { // click outside of the custom group toggleList('Shut') setState('initial') @@ -93,6 +93,23 @@ document.addEventListener('click', function(e) { // FUNCTIONS // ///////////////////////////////// +function aParentElementHasId(event, id, maxDepth) { + let currentElement = event.srcElement + let count = 0 + + while (currentElement !== undefined && currentElement !== null) { + count++ + if (currentElement.id === id) { + return true + } + if (maxDepth && count > maxDepth) { + return false + } + currentElement = currentElement.parentElement + } + return false +} + function toggleList(whichWay) { if (whichWay === 'Open') { csList.classList.remove('hidden-all') @@ -104,8 +121,7 @@ function toggleList(whichWay) { } function findFocus() { - const focusPoint = document.activeElement - return focusPoint + return document.activeElement } function moveFocus(fromHere, toThere) { @@ -188,20 +204,7 @@ function makeChoice(whichOption) { } function setState(newState) { - switch (newState) { - case 'initial': - csState = 'initial' - break - case 'opened': - csState = 'opened' - break - case 'filtered': - csState = 'filtered' - break - case 'closed': - csState = 'closed' - } - // console.log({csState}) + csState = newState } function doKeyAction(whichKey) {