Website On local network

Greetings,
I am a noob to this world of development and would like community’s insight on my problem.

If I get some data from the database using AJAX, php in a separate element, I am no longer able to access those elements using my JavaScript file that is parsed before that element.
For eg.:- I have attached a file and the browser has parsed the same.
Now, this script file contains behavior for some html

with class ‘X’. Now, the elements of class ‘X’ that were there while the document was parsed, show the defined behavior. Now, If I click a button and some more elements of class ‘X’ appear with data from a database using php and AJAX. [THE PROBLEM PART] These new elements will not adhere to the defined behavior.

I believe, the reason for the same is that, the JavaScript was defined earlier then, these elements were created.

I solved the same, by appending again a file that contains the same behavior as earlier and calling this script file after those elements are appended.

Now, I would like to know a better version for the same.