To ensure that appended elements trigger the load
event, you can attach the event to those elements using the following code:
function LOADER_FUNCTION(e) {
console.log("LOADED", e.target);
}
var element = document.querySelector('#parent');
for(i=0; i<element.childNodes.length; i++) {
element.childNodes[i].addEventListener('load', LOADER_FUNCTION);
}
The LOADER_FUNCTION
will be called when the content within the element is ready. It's interesting to note that this method does not work when attached to the parent element.
Here is a demonstration of how this works without Ajax but with similar conditions. By utilizing innerHTML
instead of createElement
-> appendChild
, I am able to show that this event is not restricted to specific methods:
(function(){
var element = document.getElementById('parent');
function LOADER_FUNCTION(e) {
document.querySelector('.status').innerText = ("LOADED " + e.target.tagName);
console.log("LOADED", e.target);
}
element.innerHTML = "<img src='http://i.imgur.com/OfSN9oH.jpg'>";
for(i=0; i<element.childNodes.length; i++){
element.childNodes[i].addEventListener('load', LOADER_FUNCTION);
}
})()
<div class='status'></div>
<div id="parent"></div>
For dealing with nested children in append or innerHTML operations, use element.querySelectorAll('*')
.
https://jsfiddle.net/bckpL9k6/1/