Created
February 16, 2017 03:43
-
-
Save ryanve/d058b1988d299a726ce587485875fe72 to your computer and use it in GitHub Desktop.
Mutation observer example listening to changes in [disabled] and [hidden] attributes
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
!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