eventListener onload and “not defined” ID error in javascript

Problem:

the function “function1” start onload with many tasks and instead “function2” is awaiting to be load after some user event.

function2 create some elements with createElement and function1 even if has a listener inside seems to answer (as obvious) with a “not defined” error blocking everything.

is there a logic way to solve this procedural mistake/error=