jQuery: how to use the DOMNodeInserted event

by gabrieleromanato

HTML

<p><button id="insert">Insert</button></p>

CSS

#test {
    padding: 1em;
    background: #ffa;
    border: 1px solid #999;
}

JavaScript

$(document).bind('DOMNodeInserted', function(e) {
    var element = e.target;
    setTimeout(function() {
        $(element).fadeOut(1000, function() {
            $(this).remove();
        });
    }, 2000);
});

$('#insert').click(function() {
    $('<div id="test"/>').
    text('Test').
    appendTo('body');
});