I'm curious about how to attach a click event to an element that will be added later through an ajax call. I know jQuery can handle this like so:
$(document).on('click', '.ajax-loaded-element' function(){});
However, I'm not currently using jQuery. My approach is similar to the one below, but it may fail if the same file is called twice (not sure if necessary, but want to cover all bases).
bindAjaxBtn: function(selector, fnName) {
let isBinded = false;
document.addEventListener('click', function(e) {
const btn = document.querySelector(selector);
if(btn === e.target) {
if(!isBinded) {
btn.addEventListener('click', function() {
fnName();
isBinded = true;
});
btn.click();
}
}
});
},
Any assistance on this matter would be greatly appreciated. :)