Answer the question
In order to leave comments, you need to log in
How to hang events on added elements?
Hello. At the moment I am working on a social network (front-end), and there is a lot of js out there, which is hung up on certain elements on the page, for example, like a post, commenting, etc. But what to do about when records are added via ajax? After all, events will not be hung up on new elements. How can I track changes in the DOM and hang events on new elements? I write everything in native, so preferably without jQuery.
Answer the question
In order to leave comments, you need to log in
No need to track changes, hang an event on the parent element, which is static.
For example:
$('body').on('click', '.comment .like', function(){ ... });
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question