0

I am fully aware of MutationObserver but what I need is different:

I would like to listen to an element which has been created in the memory, and that specific element can be appended anywhere (thus I do not want to observe everything because it would be an overkill as things gets changed everywhere on the app, all the time).

The listener would fire when the specific element has been inserted to the DOM. just a way to follow a single element instead of watching the whole DOM. it's kinda backwards approach but I think it is the right way to go in my case.

How can this be done? thanks!

vsync
  • 118,978
  • 58
  • 307
  • 400

1 Answers1

2
$myobject.appendTo('body').trigger('myobserver');

$myobject.on('myobserver',function(){
    //do stuff when myobject is added to DOM
});

You could use this CSS trick too: http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • no I don't want this. this is not a listener. This is exactly like calling a function after I've appended it to the DOM. the element can inserted to the DOM from many places in the code, so this is why I want a `listener`. – vsync Jun 11 '13 at 16:11
  • 1
    so you could extend specific jquery method used to add/remove your object from the DOM if, and only if you use jquery for that. Otherwise, there is no way other than testing for the object length inside a interval ( mutation observer is a too much performance killer & not cross browser). – A. Wolff Jun 11 '13 at 16:17
  • exaclty...I feel browsers must add native support so you would be able to follow state changes of specific DOM nodes. The Mutation observer is just like a god watching at the world, and what I need is to only watch a single person. God is an overkill in so many ways. – vsync Jun 12 '13 at 11:31
  • There are many tries to implement it but always give very bad performance issue. DOM4 draft seems to want it: https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#mutation-observers – A. Wolff Jun 12 '13 at 11:36
  • omg we are living in the stone age of internet browsers..Thanks for the response Mr. roasted. – vsync Jun 12 '13 at 11:44