Cihad Turhan
Cihad Turhan

Reputation: 2849

How to add listeners on dom node removal, attribute modifications?

I want to add a listener on dom node add/remove and attribute modifications because I mostly generate dom elements dynamically for modules.

As you see in the picture below, webkit made it so it means these listeners exists.

enter image description here

If there is any jQuery functions please inform.

Thanks.

Upvotes: 2

Views: 824

Answers (2)

pdoherty926
pdoherty926

Reputation: 10399

For attribute change events, you could try something like this.

$el.on('attribute:change', function () {...});
$el.attr('selected', 'selected').trigger('attribute:change');

For add/remove events, you could do something like this:

(function() {
    var ev = new $.Event('remove'),
    orig = $.fn.remove;

    $.fn.remove = function() {
        $(this).trigger(ev);
        return orig.apply(this, arguments);
    }
})();

Upvotes: 0

jAndy
jAndy

Reputation: 236172

Actually, you can't expect Javascript to have a method or listener for anything which webkits developer tools can do. Its a buildin debugger and console which possibilites go way beyond.

However, there are the such called Mutation Events available, see MDN

A typical call would look like

document.getElementsById( 'foo' ).addEventListener('DOMAttrModified', function( e ) {
}, false);

which would fire on any attribute change from the Node with the id foo. One word of caution, the Mutation Events are deprecated and might not be available in the current form in the future.

Upvotes: 2

Related Questions