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

  • SEARCH
  • Home
  • 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 6222091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:15:11+00:00 2026-05-24T08:15:11+00:00

I know I may be asking for the moon here but I’m looking for

  • 0

I know I may be asking for the moon here but I’m looking for some experienced opinons on the best way to add event listeners or rather ‘When’ or ‘Where’ to add them in the js file.

Take my take as an example. I have a page which has a bunch of onclick events that now have to be handled by properties in the JS file

eg

var test = document.getElementById("i-am-element");
test.onclick = testEventListener;

My question is where exactly I should add this in the js file.

How I was planning to go about it was to have something like the following

$(document).ready(function() {
    var test = document.getElementById("test-me-shane");
    test.onclick = testEventListener;

    //add all the functions to different elements
});

myfunction1(){}
myfunction2(){}
myfunction3(){}

So that once the document is ready, only then are all the event listeners added. Is this acceptable or is there are more universally accepted way of doing it.

NOTE: I know this question may appear subjective so I’m going with the correct answer will be the most popular way you’ve seen seen event listeners added. I’m sure there must be a majority acceptance on this and I apologize in advance if its similiar to something like where you should declare variables, at the start or when you need them.

In Java, should variables be declared at the top of a function, or as they're needed?

  • 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-05-24T08:15:13+00:00Added an answer on May 24, 2026 at 8:15 am

    You really want to be able to add all your event listeners in the same place; why? Simply for ease-of-maintenance.

    Because of this, the best place to put all your event listeners is in a place where you can guarantee all elements you’ll possibly want to bind event handlers to are available.

    This is why the most common place to bind your event handlers is after the DOMReady event has fired $(document).ready().

    As always, there are some exceptions to the rule. Very occasionally, you might want to bind an event handler to an element as soon as it is available; which is after the closing tag of the element has been defined. In this instance, the following snippet should be used:

    <div id="arbitrary-parent">
        <h1 id="arbitrary-element">I need an event handler bound to me <strong>immediately!</strong></h1>
        <script>document.getElementById("arbitrary-element").onclick = function () { alert("clicked"); }</script>
    </div>
    

    The other thing you should consider is how you are going to bind your handlers. If you stick to: DOMElement.onclick = function () { };, you’re limiting yourself to binding on handler per event.

    Instead, the following approach allows you to bind multiple handlers per event:

    function bind(el, evt, func) {
        if (el.addEventListener){
            el.addEventListener(evt, func, false);
        } else if (el.attachEvent) {
            el.attachEvent('on' + evt, func);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this may be basic but I cannot seem to add a member
This may be a little unconventional way of asking for help but my code
I know this may be a noob question, but it's bugging the heck out
I know this may be a dumb question, but my background is more in
As you may know, in many occasions, there is a need to flag some
Some of our end users are not IT savvy and may not know how
I may be greatly misunderstanding this threading scenario, but that's why I'm asking. What
I'm asking here not because I never used visual studio but because I'm starting
What I'm asking may sound strange, but I really need it.. What I need
This may be a server question, but I'm asking how to do this system

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.