I want to created bind event with all elements which will be generated dinamically.
Look at this code:
$('body').on('click', '.comment', () => { alert('Hello World'); });
It works only for first element. For example - if I click on the first comment, alert will display, but if I click in another it doesn't.
`const comments = document.querySelectorAll('.comment');
comments.forEach((comment) => {
comment.addEventListener('click', (e) => {
alert('Hello World');
})
})
` OK. It works, but we have a loop here and I won't use it in this way becouse I have more complicated logic than display alert and this code crashed sometimes.So the first way will be the best for me but it works only for first element. Any idea?