Last active
November 20, 2016 15:09
-
-
Save peeke/e17919bc4171b4bd29a371fcd465b67b to your computer and use it in GitHub Desktop.
Function to listen for attribute changes. Useful for preventing inconsistency between your internal module state and the DOM. (IE11+)
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 attributeObserver = (element, attr, cb) => { | |
const mutObserver = new MutationObserver(mutations => { | |
mutations.forEach(mut => { | |
mut.attributeName === attr && cb(element.getAttribute(attr)); | |
}); | |
}); | |
mutObserver.observe(element, { | |
attributes: true, | |
attributeFilter: [ attr ] | |
}); | |
return mutObserver; | |
}; | |
const handleAttrChangeFn = attrValue => console.log(attrValue); | |
const ariaHiddenObserver = attributeObserver(document.getElementById('foo'), 'aria-hidden', handleAttrChangeFn); | |
ariaHiddenObserver.disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment