Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:34:39+00:00 2026-06-04T12:34:39+00:00

This question is so basic, I’m certain in must be a duplicate of something

  • 0

This question is so basic, I’m certain in must be a duplicate of something, even though I’ve looked for something similar.

My question is basically: Where is the best place to initially register event handlers for HTML elements?

The easiest way to register an event handler is obviously to just do it inline:

<div id = "mybutton" onclick = "doSomething()">Click me</div>

But this goes against the overwhelming march towards separation of logic and content in modern web development. So, in 2012, all logic/behavior is supposed to be done in pure Javascript code. That’s great, and it leads to more maintainable code. But you still need some initial hook that hooks up your HTML elements with your Javascript code.

Usually, I just do something like:

<body onload = "registerAllEventHandlers()">

But… that’s still “cheating”, isn’t it – because we’re still using inline Javascript here. But what other options do we have? We can’t do it in a <script> tag in the <head> section, because at that point we can’t access the DOM since the page hasn’t loaded yet:

<head>
<script type = "text/javascript">
    var myButton = document.getElementById("mybutton"); // myButton is null!
</script>
</head>

Do we place a <script> tag at the bottom of the page or something? Like:

<html>
<body>
...
...
<script type = "text/javascript">
   registerAllEventHandlers();
</script>
</body>
</html>

What is the best practice here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:34:40+00:00Added an answer on June 4, 2026 at 12:34 pm

    You can use window.onload:

    <script type = "text/javascript">
      window.onload = registerAllEventHandlers;
    </script>
    

    Or if you use jquery:

    $(registerAllEventHandlers);
    

    Using onload works because it registers onload event immediately but fires it when DOM is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question gives a basic description of what I'm trying to do. Basically, I
Possible Duplicate: Can jQuery provide the tag name? Hi! This question is so basic
I'm sorry if this question is basic(I'm new to nosql). Basically I have a
I realize this question is pretty basic, but I'm really stuck. I have a
guys I know this question is very basic but I've met in few publications
Forgive my ignorance in asking this basic question but I've become so used to
I'm new to C++, so this question may be basic: I have two classes
EDIT: This question is now redundant since Twitter no longer supports basic auth. I've
Well, I'm learning Scala so this question may be too basic for most people.
This might be basic question but how do I create a list of lists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.