When I put the js code between the tag <script></script> the code will appear in the header when I inspect in the browser. However I couldn’t reference html element from JS. So I made a simple html and JS just to test:
<div>
<input type="text" id="lname" name="lname">
</div>
For JS code I insert into the hook element
Hook: wp_head
Location: Front Page
<script>
document.getElementById("lname").value = "My Value";
</script>
However when I publish the page and look it up in the browser the input tag wasn’t populated with “My Value”. When I inspect the code via browser there is an error:
Uncaught TypeError: Cannot set properties of null (setting ‘value’)
May I know how can I reference html element like input tags from JS?