Alex Rivera | Logout

on append() do something

Asked 2011-08-23T20:22:52.737
30

Is it possible to add some kind of an event/handler when an element is appended to the DOM...?

.click(), .change(), .keyup() etc. alike...

I need to get the height of an element as soon as it is appended and then set the height to another element

Edit
Report

1 Answer

68

You can override the default append method and cause it to trigger a custom append event. Then bind a handler to an element for that event: http://jsfiddle.net/H8ygx/

(function($) {
    var origAppend = $.fn.append;

    $.fn.append = function () {
        return origAppend.apply(this, arguments).trigger("append");
    };
})(jQuery);

$("div").bind("append", function() { alert('Hello, world!'); });

$("div").append("<span>");
answered 2011-08-23T20:34:11.330

Your Answer