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
what about http://jsbin.com/esepal/2
$(document).bind("DOMSubtreeModified",function(){
console.log($('body').width() + ' x '+$('body').height());
})
This event has been deprecated in favor of the Mutation Observer API
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
You can add a callback option to html (,or any) function:
$.fn.oldHtml = $.fn.html;
$.fn.html = function(html,fn){
fn = fn || function(){};
var result = this.oldHtml(html);
fn();
return result;
};
$('body').html(11,function(){alert("haha");});
Demo here.
You do the change on some element, not the element is forced to change by something that you have to catch.
And with HTML5 we have native DOM Mutation Observers.
We can achieve this by using Mutation Events. According to www.w3.org, The mutation event module is designed to allow notification of any changes to the structure of a document, including attr and text modifications. For more detail MUTATION EVENTS
For Example :
$("body").on('DOMSubtreeModified', "#content", function() {
alert('Content Modified'); // do something
});
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?