Skip to content

Instantly share code, notes, and snippets.

@ryanve
Created February 16, 2017 03:43
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 ryanve/d058b1988d299a726ce587485875fe72 to your computer and use it in GitHub Desktop.
Save ryanve/d058b1988d299a726ce587485875fe72 to your computer and use it in GitHub Desktop.
Mutation observer example listening to changes in [disabled] and [hidden] attributes
!function() {
var emitter = {
emit: console.dir.bind(console)
}
var disabled = 'disabled'
var enabled = 'enabled'
var hidden = 'hidden'
var unhidden = 'unhidden'
function emit(mutation) {
var property = mutation.attributeName
var target = mutation.target
if (property === disabled) {
emitter.emit(target[disabled] ? disabled : enabled, target)
} else if (property === hidden) {
emitter.emit(target[hidden] ? hidden : unhidden, target)
}
}
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