0

Possible Duplicate:
Most efficient method of detecting/monitoring DOM changes?

I tried this, but it doesn't works:

HTMLElement.prototype.onload = function() {
    alert(this.tagName);
};
Community
  • 1
  • 1
Hard Rain
  • 1,380
  • 4
  • 14
  • 19
  • Is there a specific element you're looking for? What is the end goal here? – hunter Dec 19 '12 at 17:42
  • 2
    That would be mutation events, and they are not very well supported. Be better to just keep track of your elements when you insert them. – adeneo Dec 19 '12 at 17:43
  • 1
    Only a few types of elements fire `load` events, specifically, those that load some kind of related resource, like `img`, `script`, and `iframe`. – apsillers Dec 19 '12 at 17:46

2 Answers2

2

Elements don't appear in documents unless you put them there.

Mutation events exist to trap inserts, deletes, etc, but they're not well supported so most of the time you're better off just trapping the insertions at the time you do them.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
2

Really, what are you trying to achieve here?

If it is only about to determine if a given HTMLElement is currently in the DOM you can refer to the element's parentNode attribute. All elements except <html> will have a parentNode when they are part of the DOM tree. And you can have elements that you created via JS but never inserted into the DOM or elements that were in the DOM but now are removed from it. Either way, one with parentNode IS currently in the DOM and one w/o is not.

marekful
  • 14,986
  • 6
  • 37
  • 59