Skip to content

Instantly share code, notes, and snippets.

@m3g4p0p
Last active July 16, 2016 22:01
Show Gist options
  • Save m3g4p0p/01c6b6b7f2bb5c85c4d25cadc3ca15e8 to your computer and use it in GitHub Desktop.
Save m3g4p0p/01c6b6b7f2bb5c85c4d25cadc3ca15e8 to your computer and use it in GitHub Desktop.
$.fn.observe = function(callback, options) {
options = options || {
attributes: true,
childList: true,
characterData: true
};
$(this).each(function() {
var observer = new MutationObserver(callback.bind(this));
observer.observe(this, options);
});
return this;
};
@m3g4p0p
Copy link
Author

m3g4p0p commented Jul 16, 2016

jQuery observe

A jQuery extension to quickly apply a mutation observer to jQuery objects. The first argument is a callback function, within which this refers to the current element; also gets the MutationRecord passed as well as the MutationObserver (just as you'd expect). The second argument optionally takes an options object with the usual MutationObserverInit API.

Sample usage:

$('body').observe(function(mutations, observer) {
    console.log('Change observed');
    observer.disconnect();
}).append('<div/>');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment