How to find and change a dynamic element in Javascript?

On the page dynamically added elements.
Need to dynamically created elements add class. If you use static elements I do this:

var li = document.querySelector("elementname");
li.style.classList.add("newclass");


How to do it in dynamic created elements ?
April 3rd 20 at 17:27
4 answers
April 3rd 20 at 17:29
Solution
Implemented well, works.

$.ajaxSetup({
 complete: function() {

$("element").addClass('newclass');
}
})
April 3rd 20 at 17:31
MutationObserver allows you to monitor DOM changes.

This is for "heavy cases" where you do not affect the code that adds the elements.
April 3rd 20 at 17:33
1) to Generate the event when adding new elements. Well, this event to do what you do
2) setInterval'ω every N-seconds to look for new elements and add class.
I think that, in the latter case it will create additional load on the client computer - gina.Litt commented on April 3rd 20 at 17:36
April 3rd 20 at 17:35
in the piece, where they "dynamically added" (comes from the server, response with ready html?), there, they immediately add class?

div.innerHTML += responseFromServer

[...div.querySelectorAll('.some-class-name')].forEach(d => d.classList.add('new-class-name'))
no, html does not come, and just dynamically add the class "active". Actually the problem is to find an element with a dynamically added "active", and add to it a new class. - gina.Litt commented on April 3rd 20 at 17:38
If the class is active with animation or transistor, it is possible to hang listener transitionend or animationend, respectively. Well, or if there is click event can be.target test item. - tad.McLaughlin commented on April 3rd 20 at 17:41

Find more questions by tags JavaScript