Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Error when JS in <head> tried to click on doc element in <body> (How to defer parsing?)

I've got JS code in my <head> that I'd like to click an element in the <body>. This is causing errors even though I've tried to postpone running the JS by:

  • calling 'defer'
  • using 'document.onload'
  • using 'window.onload'
to postpone the execution.

Is there any way to have my JS in the <head> or do I just need to move it to the bottom of the <body>?

Here's a test page showing the issue:

  <head>
    <script defer>
      document.addEventListener(
        "DOMContentLoaded",
        document.getElementById("my_item").click()
      );
    </script>
  </head>
  <body>
    <h1 id="my_item">Testing Defer JS</h1>
    <script></script>
  </body>
</head>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!