10

My question is somewhat weird, but can i add events for any DOM elements(div), like 'onHtmlChange', to be notified when that div has changed their content?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Frangossauro
  • 4,446
  • 4
  • 23
  • 15
  • Ideally, you shouldn't ever require such an event. What exactly would make the HTML of any given element change? – James Aug 24 '09 at 22:02
  • GMaps is feeding an DIV with his content. But the root of my question is just to mimics a dataflow programming approach :P – Frangossauro Aug 24 '09 at 23:33

2 Answers2

8

Check out DOMNodeInserted and DOMNodeRemoved.

Ben Nadel recently blogged the following: Detecting When DOM Elements Have Been Removed With jQuery

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Seems like the OP wants to know when HTML content inside of the divs has changed though. – seth Aug 24 '09 at 20:58
  • @seth —Browsers which use the W3C model should fire both `DOMNodeInserted` and `DOMNodeRemoved` when changing a node's contents, even it it's just text nodes (just tested FF3). The IE trick mentioned in the linked article could easily be extended to `.html()` and `.text()` to mimic this. – Ben Blank Aug 24 '09 at 21:12
1

The DOMNodeInserted and DOMNodeRemoved are part of the Mutation Events which is now deprecated. Hence, you need to take a look on Mutation Observer. I did the same in one of my project which is I needed to listen to the DOM changes of a certain element., e.g. DOM append, remove, attribute, and data changes.

These links will help you :

  1. http://gabrieleromanato.name/jquery-detecting-new-elements-with-the-mutationobserver-object/
  2. https://github.com/kapetan/jquery-observe
  3. Is there a JavaScript/jQuery DOM change listener?
Community
  • 1
  • 1