AddEventListener is not working for me. This is what I have tried:
const mtRemoveKeywords = document.querySelectorAll('.mt-remove-keyword');
for (let mtRemoveKeyword of mtRemoveKeywords) {
mtRemoveKeyword.addEventListener('click', (e) => {
alert('JS');
});
}
I do not get an error message, so nothing happens. I guess this is because the selector is a dynamic element. I also tried it with windows.onload
but that didn't help. The weird thing is that I have tried it with jQuery and that worked!
$(document).on('click', '.mt-remove-keyword', function() {
alert('jQuery');
});
Why is it not working with Vanilla JS?