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 how can I detect any changes on my div at any point in time ?
I dont want to use any intervals or default value checked.
Something like this would do
$('mydiv').contentchanged() {
alert('changed')
}
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()
.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 the OpenClosed priciple. :)
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.
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,
Now you can subscribe divhtmlchanging/divhtmlchanged events as follow,
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;
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).
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.
Since
$("#selector").bind()
is deprecated, you should use:Following code works for me.
Hope it will help someone :)