I'm looking for a better way to handle events for multiple elements on a page that can appear dynamically. Currently I'm using this code:
var myToolbar = document.createElement('div');
setInterval(function () {
for (let textField of document.querySelectorAll('textarea[name="text"]:not(.modified)')) {
textField.classList.add('modified');
textField.addEventListener('focus', function () {
textField.insertAdjacentElement('beforebegin', myToolbar);
});
}
}, 1000);
This accomplishes my task fine, but having an interval running all the time seems clunky.