When you moved the script from being inline to being external you also moved the script tag to the .
Now the script runs before any element that matches div:not(:first-child) p exists so first_container doesn't have any elements in it.
Move the script element back to where it was before.
Alternatively, move the code you have into a function and then run that function after the document has loaded (e.g. with addEventListener('load', your_function)).