Skip to content

Instantly share code, notes, and snippets.

@ryanve
Created February 16, 2017 04:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryanve/2150bcfe285ac9784d79fed9e8714d9c to your computer and use it in GitHub Desktop.
Save ryanve/2150bcfe285ac9784d79fed9e8714d9c to your computer and use it in GitHub Desktop.
Mutation observer example listening to any attribute changes
!function() {
var emitter = {
emit: console.dir.bind(console)
}
function emit(mutation) {
var target = mutation.target
var name = mutation.attributeName
var value = target.getAttribute(name)
emitter.emit({
mutation: mutation,
target: target,
name: name,
value: value,
state: value != null
})
}
var observer = new MutationObserver(function(mutations) {
mutations.forEach(emit)
});
observer.observe(document.body, {
subtree: true,
attributes: true
});
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment