change()
function works and detects changes on form elements, but is there a way of detecting when a DOM element\'s content was changed?
This does not w
Not possible, I believe ie has a content changed event but it is certainly not x-browser
Should I say not possible without some nasty interval chugging away in the background!
I know this post is a year old, but I'd like to provide a different solution approach to those who have a similar issue:
The jQuery change event is used only on user input fields because if anything else is manipulated (e.g., a div), that manipulation is coming from code. So, find where the manipulation occurs, and then add whatever you need to there.
But if that's not possible for any reason (you're using a complicated plugin or can't find any "callback" possibilities) then the jQuery approach I'd suggest is:
a. For simple DOM manipulation, use jQuery chaining and traversing, $("#content").html('something').end().find(whatever)....
b. If you'd like to do something else, employ jQuery's bind
with custom event and triggerHandler
$("#content").html('something').triggerHandler('customAction');
$('#content').unbind().bind('customAction', function(event, data) {
//Custom-action
});
Here's a link to jQuery trigger handler: http://api.jquery.com/triggerHandler/
It's not strictly a jQuery answer - but useful to mention for debugging.
In Firebug you can right-click on an element in the DOM tree and set up 'Break on Attribute Change':
When an attribute is changed in a script, the debug window will appear and you can track down what it going on. There is also an option for element insertion and element removal below (unhelpfully obscured by the popup in the screengrab).
Often a simple and effective way to achieve this is to keep track of when and where you are modifying the DOM.
You can do this by creating one central function that is always responsible for modifying the DOM. You then do whatever cleanup you need on the modified element from within this function.
In a recent application, I didn't need immediate action so I used a callback for the handly load() function, to add a class to any modified elements and then updated all modified elements every few seconds with a setInterval timer.
$($location).load("my URL", "", $location.addClass("dommodified"));
Then you can handle it however you want - e.g.
setInterval("handlemodifiedstuff();", 3000);
function handlemodifiedstuff()
{
$(".dommodified").each(function(){/* Do stuff with $(this) */});
}
I'm developing tiny JS library called mutabor (https://github.com/eskat0n/mutabor) which intended to simplify usage of DOM Mutation Events. See demo.html for examples.
These are mutation events.
I have not used mutation event APIs in jQuery, but a cursory search led me to this project on GitHub. I am unaware of the project's maturity.