views:

4064

answers:

8

change() function works and detects changes on form elements, but can I have a way of detecting when a html content was changed?

This is not working, unless #content is an input fields

$("#content").change( function(){
    // do something
});

I want this to trigger when doing something like:

$("#content").html('something');

Also html() or append() function don't have a callback.

Any suggestions?

A: 

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!

redsquare
impossible is nothing!
Here Be Wolves
+6  A: 

you must be talking about mutation events.

I have not used any mutation event APIs in jquery. but a cursory googleing led me to this project on github. I an unaware of the maturity level of this plugin.

But by what I've seen, a lot of projects on github are pretty decent.

Cheers!

Here Be Wolves
"... a lot of projects on github are pretty decent." I don't think one can make an assumption that grand on the contents of a source control site, regardless of who it is.
Danny
that may as well be the case, yes, but jollytoad's projects are pretty good: I follow him!
Here Be Wolves
This mutation events plugin won´t work, because it just hooks the events into the jQuery mutation methods. They are fired when jQuery itself tries to change the elements, but not when the elements gets changed from outside jQuery. They don't watch changes on the document.Check out this little plugin instead:http://stackoverflow.com/questions/3233991/jquery-watch-div/3234646#3234646
Sebastián Grignoli
+1  A: 

Try to bind to the DOMSubtreeModified event seeign as test is also just part of the DOM.

see this post here on SO:

how-do-i-monitor-the-dom-for-changes

Colin
+5  A: 

Try this, it was created by James Padolsey(J-P here on SO) and does exactly what you want (I think)

http://james.padolsey.com/javascript/monitoring-dom-properties/

Pim Jager
+1  A: 

The browser will not fire the onchange event for <div> elements.

I think the reasoning behind this is that these elements won't change unless modified by javascript. If you are already having to modify the element yourself (rather than the user doing it), then you can just call the appropriate accompanying code at the same time that you modify the element, like so:

 $("#content").html('something').each(function() { });

You could also manually fire an event like this:

 $("#content").html('something').change();

If neither of these solutions work for your situation, could you please give more information on what you are specifically trying to accomplish?

TM
A: 

Try the livequery plugin. That seems to work for something similar I am doing.

http://docs.jquery.com/Plugins/livequery

Arpit
+1  A: 

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) */});
}
Antony Carthy
+1  A: 

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:

  1. 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.

  2. 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/

Emile
final thought. although that should be pretty comprehensive above: you could update the value of a hidden input field with that of the #content html and then bind the change event to the hidden input field :) lots of options
Emile