Created
August 29, 2018 01:29
-
-
Save asvny/20f444a5018d3444b57b4c84a651f24d to your computer and use it in GitHub Desktop.
Autofocus for all HTML Elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(_ => { | |
const target = document.documentElement; | |
// config object | |
const config = { | |
attributes: true, | |
attributeOldValue: true, | |
characterData: true, | |
characterDataOldValue: true, | |
childList: true, | |
subtree: true | |
}; | |
// subscriber function | |
function subscriber(mutations) { | |
mutations.forEach(mutation => { | |
let { target } = mutation; | |
if ( | |
target.nodeType === Node.ELEMENT_NODE && | |
target.hasAttribute("focus") | |
) { | |
target.focus(); | |
} | |
}); | |
} | |
// instantiating observer | |
const observer = new MutationObserver(subscriber); | |
// observing target | |
observer.observe(target, config); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment