0

Everything else listed online either doesn't seem to work or is now deprecated. Ive heard of mutation observers but can't find sufficient documentation online anywhere.

It's a normal tag whose contents are updated by a js file. I need to be able to detect every change on this div that is injected and apply a css property to the div when the text changes. (It does this often, approx once every 2 seconds).

Anyone here know how to approach this?

Rohan Kapur
  • 139
  • 4
  • 13
  • If it is updated with a database values then you might look into websockets. – Jai Oct 09 '18 at 06:56
  • 3
    Assuming there are no events raised by the logic which updates the HTML, then you could certainly use a MutationObserver. There's lots of documentation at MDN: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver, although as @Jai recommends, if you need to keep the client UI in sync with server data with a latency of ~2 seconds, WebSockets or Server Sent Events would be a much better idea. – Rory McCrossan Oct 09 '18 at 06:57
  • @Jai that has absolutely nothing to do with what I asked. I need to be able to detect changes made in the DOM of a divider using javascript and then apply a css property to said div. – Rohan Kapur Oct 09 '18 at 06:58
  • It does. Assuming you can change how the data is provided to the page, a socket connection would be a much better than polling every 2 seconds. – Rory McCrossan Oct 09 '18 at 06:59
  • Typically looks like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), hence why you get comments that you may not understand how they can be related to your issue description. You have a script (maybe Vue since you tagged it) that modifies DOM, and you want to apply further modifications (CSS) when that happens. So you try to detect DOM modification (=> MutationObserver) and ask for help on that. But in fact you need to modify your script so that it does your extra modification at the same time it changes the DOM. – ghybs Oct 09 '18 at 07:06
  • Related - [Detect changes in the DOM](https://stackoverflow.com/a/14570614/104380) – vsync Oct 09 '18 at 07:52

0 Answers0