I think I see what you're trying to do. In that sample, the gallery is built by appending text together. You can do something like this to add event listeners to those elements.
function createGallery(items) {
cleanup();
var htmlFragment = "";
items.results.forEach(function(item) {
htmlFragment +=
'<div class="esri-item-container">' +
(item.thumbnailUrl
? '<div class="esri-image" style="background-image:url(' +
item.thumbnailUrl +
');"></div>'
: '<div class="esri-image esri-null-image">Thumbnail not available</div>') +
(item.title
? '<div class="esri-title">' + (item.title || "") + "</div>"
: '<div class="esri-title esri-null-title">Title not available</div>') +
"</div>";
});
document.getElementById("itemGallery").innerHTML = htmlFragment;
var elements = Array.from(document.querySelectorAll(".esri-item-container"));
elements.forEach(element => {
element.addEventListener("click", clickHandler);
});
}
function cleanup() {
var elements = Array.from(document.querySelectorAll(".esri-item-container"));
elements.forEach(element => {
element.removeEventListener("click", clickHandler);
});
}
function clickHandler(event) {
}
Rather than try to use a global method, it's better to handle it like this. Using a named function for the handler lets you remove the listener as well.