I have a situation where a portion of the page is built based on a javascript action when the page is loading. In the code that is loaded there is an anchor tag. I am putting a click event listener on the a tag with jQuery.

Originally I was just doing that listener somewhere in the .js files that load initially. It wasn't working. I moved the listener declaration to within the function that actually calls the replaceWith function and put it after the replaceWith command and now it works.

This means that now it will be declared whenever the function containing the replaceWith function is called and the content changes but it works. Is that the only way this works?

I suspect that when I was doing it originally, the anchor tag didn't exist. So I dynamically added the anchor tag and the event listener didn't recognize it. Is that what is happening?