onInsert

A micro-library to trigger an event upon HTML element insertion into another parent. Best is to use $(document).on('insert', selector, func); with jQuery. Could be probably easily adapted to other frameworks.

HTML

https://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/ <br/></br/>questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloadedhttps://cdof.com.br/index.cgi/de/00/http/stackoverflow.com/questions/tagged/domcontentloaded

JavaScript

(function ( $ ) {
    var oldAppendChild = Node.prototype.appendChild,
        oldInsertBefore= Node.prototype.insertBefore;
    
    Node.prototype.appendChild = function ( child ) {
        $(child).trigger('beforeinsert', { parent : this });
        var result = oldAppendChild.call(this, child);
        $(child).trigger('insert', { parent : this });
        return result;
    };
    
    Node.prototype.insertBefore = function ( before, child ) {
        $(child).trigger('beforeinsert', { parent : this });
        var result = oldInsertBefore.call(this, before, child);
        $(child).trigger('insert', { parent : this });
        console.log(77777)
        return result;
    };
    
    $(document).on('beforeinsert insert', '#test', function ( jqEvt, info ) {
        $(jqEvt.target).text('Insert event triggered, yay!');
    });
    
    $(function () {
        $('<div>').attr('id', 'test').appendTo(document.body);
    });
    
}(jQuery));