Created
November 13, 2019 07:14
-
-
Save peeke/d5ed2e3269b251201c321b6ed0cf6934 to your computer and use it in GitHub Desktop.
Detect changes in an attribute's value
This file contains 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
export function observeAttribute(element, attribute, callback) { | |
const observer = new MutationObserver(mutations => { | |
mutations | |
.filter(x => x.type === 'attributes' && x.attributeName === attribute) | |
.forEach(_ => callback(element.getAttribute(attribute))) | |
}) | |
observer.observe(element, { attributes: true }) | |
return observer.disconnect | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment