Skip to content

Instantly share code, notes, and snippets.

@asvny
Created August 29, 2018 01:29
Show Gist options
  • Save asvny/20f444a5018d3444b57b4c84a651f24d to your computer and use it in GitHub Desktop.
Save asvny/20f444a5018d3444b57b4c84a651f24d to your computer and use it in GitHub Desktop.
Autofocus for all HTML Elements
(_ => {
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