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
You can try this
$('.myDiv').bind('DOMNodeInserted DOMNodeRemoved', function() {
});
but this might not work in internet explorer, haven't tested it
Try the MutationObserver:
browser support: http://caniuse.com/#feat=mutationobserver
<html>
<!-- example from Microsoft https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/dev-guide/dom/mutation-observers/ -->
<head>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
// Inspect the array of MutationRecord objects to identify the nature of the change
function mutationObjectCallback(mutationRecordsList) {
console.log("mutationObjectCallback invoked.");
mutationRecordsList.forEach(function(mutationRecord) {
console.log("Type of mutation: " + mutationRecord.type);
if ("attributes" === mutationRecord.type) {
console.log("Old attribute value: " + mutationRecord.oldValue);
}
});
}
// Create an observer object and assign a callback function
var observerObject = new MutationObserver(mutationObjectCallback);
// the target to watch, this could be #yourUniqueDiv
// we use the body to watch for changes
var targetObject = document.body;
// Register the target node to observe and specify which DOM changes to watch
observerObject.observe(targetObject, {
attributes: true,
attributeFilter: ["id", "dir"],
attributeOldValue: true,
childList: true
});
// This will invoke the mutationObjectCallback function (but only after all script in this
// scope has run). For now, it simply queues a MutationRecord object with the change information
targetObject.appendChild(document.createElement('div'));
// Now a second MutationRecord object will be added, this time for an attribute change
targetObject.dir = 'rtl';
</script>
</body>
</html>
You are looking for MutationObserver or Mutation Events. Neither are supported everywhere nor are looked upon too fondly by the developer world.
If you know (and can make sure that) the div's size will change, you may be able to use the crossbrowser resize event.
Adding some content to a div, whether through jQuery or via de DOM-API directly, defaults to the .appendChild()
function. What you can do is to override the .appendChild()
function of the current object and implement an observer in it. Now having overridden our .appendChild()
function, we need to borrow that function from an other object to be able to append the content. Therefor we call the .appendChild()
of an other div to finally append the content. Ofcourse, this counts also for the .removeChild()
.
var obj = document.getElementById("mydiv");
obj.appendChild = function(node) {
alert("changed!");
// call the .appendChild() function of some other div
// and pass the current (this) to let the function affect it.
document.createElement("div").appendChild.call(this, node);
}
};
Here you can find a naïf example. You can extend it by yourself I guess. http://jsfiddle.net/RKLmA/31/
By the way: this shows JavaScript complies the OpenClosed priciple. :)
You can store the old innerHTML of the div in a variable. Set an interval to check if the old content matches the current content. When this isn't true do something.
DOMSubtreeModified is not a good solution. It's can cause infinite loops if you decide to change the DOM inside the event handler, hence it has been disabled in a number of browsers. MutationObserver will be the answer.
MDN Doc
const onChangeElement = (qSelector, cb)=>{
const targetNode = document.querySelector(qSelector);
if(targetNode){
const config = { attributes: true, childList: false, subtree: false };
const callback = function(mutationsList, observer) {
cb($(qSelector))
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}else {
console.error("onChangeElement: Invalid Selector")
}
}
And you can use it,
onChangeElement('mydiv', function(jqueryElement){
alert('changed')
})