-1

What would be the best way to check for new elements on a specific part of a webpage, looping infinitely? When a new element is added/found, the script continues.

I'm currently thinking about using function checkDOMChange() for this purpose, but i'm not sure if it would work.

As you can see, i'm new to JavaScript and coding in general.

Notatbok
  • 1
  • 1

1 Answers1

0

You can use DOMNodeInserted event to detect a creation of new element.

Fiddle

Here, I've created an example that has a 5s delay before a new div is created.

HTML:

<div id="main">This is...... I don't know. Something</div><br />

JavaScript:

var elem = document.getElementById('main');

setTimeout(function() {
    var e = document.createElement('div');
    elem.appendChild(e.appendChild(document.createTextNode('Newly created Div')))
}, 5000);

// this will be triggered if any new element is appended to div#main
elem.addEventListener("DOMNodeInserted", function (ev) {
  alert('New element detected')
}, false);
Weafs.py
  • 22,731
  • 9
  • 56
  • 78
  • How would the code be if i just want to detect the creation of a new element on the page, in general? – Notatbok Nov 20 '14 at 00:14