Here's jsFiddle for sample testing
I am currently writing a jquery snippet to handle any change to the html content in the DOM called by any jquery domManip function (extension of some functions). Not sure if this is the best way to do this, so any advice would be appreciated. This snippet works as expected if attached to a document. However, if I try to bind it to a specific element, I run into a problem that works like .remove () . This may be due to the fact that the user event does not use normal propagation behavior, but I'm really not sure.
This is a working sample, I bind the contentChange event to the document, the cross browser works, since I can test it: {Firefox, IE9, Chrome and Safari under Win7}
; (function ($) { $.fn.contentChange = function (types, data, fn) { return this.on('contentChange', types, null, data, fn); }; var oDomManip = $.fn.domManip, oHtml = $.fn.html, oEmpty = $.fn.empty, oRemove = $.fn.remove, extendFct = function (oFct, sender, args) { return oFct.apply(sender, args), $.event.trigger('contentChange');
I use: $.event.trigger('contentChange') to fire a custom event.
Called as:
$(document).contentChange(function () { console.log("onContentChange") });
However, if I use:
$('#test').contentChange(function () { console.log("onContentChange") });
Custom event does not fire. Thus, to trigger a custom event for a specific element, I can fire it as follows:
$(sender).trigger('contentChange');
But now, calling the remove() method for myself or the children does not raise my custom event. I can understand that the event callback function is not called if I delete the element, but why is it not called when the children are deleted (while it works, if it is attached to the document!)?
I was expecting this line to make custom event bubbles in '#test':
$('#test').find('div:first').remove();
Is there a way to trigger this custom event associated with a specific element when manipulating this element and / or its children?