Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on append() do something

Tags:

jquery

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

like image 856
clarkk Avatar asked Aug 23 '11 20:08

clarkk


2 Answers

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>"); 
like image 159
Dennis Avatar answered Sep 21 '22 09:09

Dennis


For that you can use the "DOMSubtreeModified" event. But it has limited compatibility. See: http://www.quirksmode.org/dom/events/#t18

$('#myDiv').live("DOMSubtreeModified", function() {      alert('something changed inside #myDiv div'); }); 

This event handler will work with all standard DOM modifying methods: appendTo(), insertBefore(), insertAfter(), etc.

like image 41
Scherbius.com Avatar answered Sep 23 '22 09:09

Scherbius.com