Answer the question
In order to leave comments, you need to log in
How to attach a handler to a dynamic element?
The user enters the platform with an online lesson, at a certain moment he has a button.
In this button, you need to pull up the location.search value, I did it. But when entering the page, the user does not have this element with a button. He will appear at an unknown time.
I read that you can somehow delegate the handler to the parent, but did not find how to do it.
DOM before the button appears:
DOM after the button appears:
As code:
<div id="files" class="files panelitem mainbg" style="display:none;"></div>
Answer the question
In order to leave comments, you need to log in
And what about the event handler, if you need to change href immediately, and not at some event?
The correct answer is: find the function that adds this button and add a href change to it.
Bad answer: MutationObserver .
Event delegation.
you weigh the event on the parent and check that the click was on the desired element
https://learn.javascript.ru/event-delegation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question