I have a function that grabs an XML document and transforms it according to an XSL document. It then places the result into a div with the id laneconfigdisplay
Though the event DOMSubtreeModified is deprecated, its working as of now, so for any makeshift projects you can use it as following.
$("body").on('DOMSubtreeModified', "#mydiv", function() {
alert('changed');
});
In the long term though, you'll have to use the MutationObserver API.
You can opt to create your own custom events so you'll still have a clear separation of logic.
Bind to a custom event:
$('#laneconfigdisplay').bind('contentchanged', function() {
// do something after the div content has changed
alert('woo');
});
In your function that updates the div:
// all logic for grabbing xml and updating the div here ..
// and then send a message/event that we have updated the div
$('#laneconfigdisplay').trigger('contentchanged'); // this will call the function above
The change
event is limited to input
, textarea
& and select
.
See http://api.jquery.com/change/ for more information.
There is an excellent jquery plugin, LiveQuery, that does just this.
Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated.
For example you could use the following code to bind a click event to all A tags, even any A tags you might add via AJAX.
$('a').livequery('click', function(event) {
alert('clicked');
return false;
});
Once you add new A tags to your document, Live Query will bind the click event and there is nothing else that needs to be called or done.
Here is a working example of its magic...
Try this
$('#D25,#E37,#E31,#F37,#E16,#E40,#F16,#F40,#E41,#F41').bind('DOMNodeInserted DOMNodeRemoved',function(){
// your code;
});
Do not use this. This may crash the page.
$('mydiv').bind("DOMSubtreeModified",function(){
alert('changed');
});
http://api.jquery.com/change/
change does only work on input form elements.
you could just trigger a function after your XML / XSL transformation or make a listener:
var html = $('#laneconfigdisplay').html()
setInterval(function(){ if($('#laneconfigdisplay').html() != html){ alert('woo'); html = $('#laneconfigdisplay').html() } }, 10000) //checks your content box all 10 seconds and triggers alert when content has changed...