In jQuery how can I watch a div to determine if it has changed so that I can rebind events and perform some other action as needed?
Asked
Active
Viewed 311 times
1
-
What do you mean with "changed"? And why do you have to rebind events then? – Aletheios Aug 08 '12 at 21:17
-
Change as in elements are appended\removed from it – user974896 Aug 08 '12 at 21:24
-
1why don't you use a function after appending/removing? – Taha Paksu Aug 08 '12 at 21:33
-
because a class appends and removes. I found the answer http://stackoverflow.com/questions/3233991/jquery-watch-div – user974896 Aug 08 '12 at 21:44
2 Answers
1
If the div
has dynamic content I would like to tell you about event delegation.
Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent.1
To use event delegation in jQuery you use the on method and provide a selector
argument.

Jonas G. Drange
- 8,749
- 2
- 27
- 38
-
1Event delegation is a great solution for using events with dynamic content. – Tim Banks Aug 09 '12 at 00:27
-
Thank you Tim. "Dynamic content" was the concept I was searching for. Answer updated. – Jonas G. Drange Aug 09 '12 at 07:29
0
You could use DOMSubtreeModified event(see here)
$('my-selector').bind('DOMSubtreeModified', function() {
console.log('Children have been modified');
});
I haven't checked the supported nature of this event but in chrome it works.
Unfortunately : Why is the DOMSubtreeModified event deprecated in DOM level 3?