Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break on "innerHTML" changes in Chrome

Chrome's developer tools provides the option to break the javascript code execution when an element's attributes or DOM tree are modified. (Inspect an element > right-click on the element tag > "Break on…")
However, I would like to jump into the code when the innerHTML of an element is changed by JavaScript. Activating all the "break on" options won't do it, so I'd like to know if there is some way to do it.

like image 533
Protector one Avatar asked Jun 16 '14 12:06

Protector one


People also ask

What can I use instead of innerHTML?

For that reason, it is recommended that instead of innerHTML you use: Element.SetHTML() to sanitize the text before it is inserted into the DOM.

Why innerHTML does not work?

People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.

How do you change the properties of elements using innerHTML and style node properties?

Setting the innerHTML property of an element To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.


2 Answers

  1. Text node is a child node.
  2. To change a text, the browser remove the old text child node.
  3. Subtree Modifications is a flag. When this flag is on, you can listen
    • Attribute modifications with subtree
    • Node removal with subtree

So, You should check two. node removal and subtree modifications.

Also, you can use MutationObserver API directly.

Old events (DOMSubtreeModified , DOMCharacterDataModified, ...) are deprecated. mdn, google

like image 100
Jehong Ahn Avatar answered Sep 23 '22 16:09

Jehong Ahn


I would suggest trying DOMSubtreeModified Event .

$("#elem").on("DOMCharacterDataModified", function(){
    alert("Modified");
});

Fiddle

like image 34
Alexander Avatar answered Sep 20 '22 16:09

Alexander