Query Selector on multiple pages

let newProjectForm = document.getElementById("newProjectForm");
projectTitle.textContent = "project";
<div>
    <h1 id="projectTitle">Title</h1>


</div>

I have a website that has multiple pages (all of them are .php). I have a selector in my javascript that selects based on element id and replaces the text but it says in the console that it cannot retrieve the value of null. I used the syntax document.getElementById("projectTitle"). Is there a different way to target the page I want to get the element from or is there something else I’m missing?
Also, the header with the links to the .css and .js files are in a separate .php file that is included at the start. The css works.
I added a snippet of the code but the issue is not clear as it works perfectly in the snippet. My pages are, however, .php and about 7 of them in the entire website.