I have a div which has its content changing all the time , be it ajax requests
, jquery functions
, blur
etc etc.
Is there a way
If you don't want use timer and check innerHTML you can try this event
$('mydiv').bind('DOMSubtreeModified', function(){
console.log('changed');
});
More details and browser support datas are Here.
Attention: in newer jQuery versions bind() is deprecated, so you should use on() instead:
$('body').on('DOMSubtreeModified', 'mydiv', function(){
console.log('changed');
});
Tried some of answers given above but those fires event twice. Here is working solution if you may need the same.
$('mydiv').one('DOMSubtreeModified', function(){
console.log('changed');
});
Since $("#selector").bind() is deprecated, you should use:
$("body").on('DOMSubtreeModified', "#selector", function() {
// code here
});
Following code works for me.
$("body").on('DOMSubtreeModified', "mydiv", function() {
alert('changed');
});
Hope it will help someone :)
Use MutationObserver as seen in this snippet provided by Mozilla, and adapted from this blog post
Alternatively, you can use the JQuery example seen in this link
Chrome 18+, Firefox 14+, IE 11+, Safari 6+
// Select the node that will be observed for mutations
var targetNode = document.getElementById('some-id');
// Options for the observer (which mutations to observe)
var config = { attributes: true, childList: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
for(var mutation of mutationsList) {
if (mutation.type == 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type == 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
observer.disconnect();
There is no inbuilt solution to this problem, this is a problem with your design and coding pattern.
You can use publisher/subscriber pattern. For this you can use jQuery custom events or your own event mechanism.
First,
function changeHtml(selector, html) {
var elem = $(selector);
jQuery.event.trigger('htmlchanging', { elements: elem, content: { current: elem.html(), pending: html} });
elem.html(html);
jQuery.event.trigger('htmlchanged', { elements: elem, content: html });
}
Now you can subscribe divhtmlchanging/divhtmlchanged events as follow,
$(document).bind('htmlchanging', function (e, data) {
//your before changing html, logic goes here
});
$(document).bind('htmlchanged', function (e, data) {
//your after changed html, logic goes here
});
Now, you have to change your div content changes through this changeHtml()
function. So, you can monitor or can do necessary changes accordingly because bind callback data argument containing the information.
You have to change your div's html like this;
changeHtml('#mydiv', '<p>test content</p>');
And also, you can use this for any html element(s) except input element. Anyway you can modify this to use with any element(s).