At a certain point a new div is created that did not exist on the page beforehand
document.getElementById("LightBoxNumber").innerHTML = "<div class="WinnerBanner">Congrats to all Winners!</div>";
from
<div id="LightBoxNumber" class="LightBoxShadow" style="font-size: xxx-large;"></div>
To
<div id="LightBoxNumber" class="LightBoxShadow" style="font-size: xxx-large;"><div class="WinnerBanner">Congrats!</div></div>
I would like to run a separate to append into that div at the moment it is created
i.e. create <span></span>
<div id="LightBoxNumber" class="LightBoxShadow" style="font-size: xxx-large;"><div class="WinnerBanner">Congrats!</div><span></span></div>
I have tested a few options and ways but cannot figure this out
$(document).ready(function(){
$('.WinnerBanner').each(function(){
if($(this).find('.WinnerBanner').length > 0){
$(this).prepend('Yes!');
}
});
});
$(document).ready(function() {
if( $('div.WinnerBanner').length > 0 ){
$('#SelectNumbersBox').append('HHHHHHHHHHHH');
}
$('#SelectNumbersBox').append('DDDDDDDDDD');
});
$('body').change(function(event){
if( $('div.WinnerBanner').length > 0 ){
$('#SelectNumbersBox').append('HHHHHHHHHHHH');
}
$('#SelectNumbersBox').append('DDDDDDDDDD');
});
2
Answers
You can use a mutation observer to watch the parent div for changes and then run your code when it changes
You can use a MutationObserver to detect when the dynamic content is injected in to the DOM and make your own updates based on that event.
Here’s a basic working example of how to do that: