You can use Mutation Events , but events have been flagged as deprecated in the DOM Events specification because the design of the API is erroneous.
The MutationEvent interface was introduced in DOM Level 2 Events, but has not yet been fully and functionally implemented in all user agents. In addition, there were criticisms that the interface, as provided, poses a performance and implementation problem. DOM4 provides a new mechanism using the MutationObserver interface, which takes into account the use cases that mutational events solve, but in a more advanced way. Thus, this specification describes mutation events for reference and the completeness of obsolete behavior, but does not allow the use of the MutationEvent interface.
One simple parameter uses the .trigger() method:
var $div = $(div).on('appendedToDOM', function() { console.log('appended'); });
You can also create a helper function that adds an element and fires a custom event:
function append(elem, target, cEvent) { if (typeof target === 'undefined') var target = document.body; if (typeof cEvent === 'undefined') var cEvent = 'appendedToDOM'; $(elem).appendTo(target).trigger(cEvent); }
source share