Mutation Observer - Generic Example
HTML
<div>hello</div>
<div>test</div>
JavaScript
(function( $ ){
$.fn.observe = function( callback, options ) {
var settings = $.extend( {
attributes: true,
childList: true,
characterData: true
},
options );
return this.each( function() {
var self = this,
observer,
MutationObserver = window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
if ( MutationObserver && callback ) {
observer = new MutationObserver(function( mutations ) {
callback.call( self, mutations );
});
observer.observe( this, settings );
}
});
};
})( jQuery );
var bizamajig__mutation_observer__helper = function ( m ) {
// console.log( this, m.attributeName, m.target.outerHTML );
console.log( m );
},
options = { characterData: true };
// usage:
$('div').observe( bizamajig__mutation_observer__helper, options ).addClass( 'yatta' );
// test if the observer is still here...
setTimeout( function () {
$( 'div' ).removeClass( 'yatta' ).addClass( 'Genki desu' ).addClass( 'oTsukareSamaDesu' )},
5000);