Skip to content

Instantly share code, notes, and snippets.

@peeke
Created November 13, 2019 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peeke/d5ed2e3269b251201c321b6ed0cf6934 to your computer and use it in GitHub Desktop.
Save peeke/d5ed2e3269b251201c321b6ed0cf6934 to your computer and use it in GitHub Desktop.
Detect changes in an attribute's value
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