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 8008743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:13:56+00:00 2026-06-04T18:13:56+00:00

Ok, so I tried to make this page with an addEventListener function, to address

  • 0

Ok, so I tried to make this page with an addEventListener function, to address the clicking of a button; It didn’t work. I narrowed it down by deleting everything but the basic elements needed for the listener, and am left with the following:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function init(){
    document.getElementById("test").addEventListener("mousedown", function(){
        alert("Test successful");
    });
}
body.onload = init();
</script>
</head>
<body>
<button id="test">Click</button>
</body>
</html>

which didn’t work either, so I know the issue is in my syntax of the above. Mind you, when I took away body.onload = init(); and instead put onload="init()" into the body tag, it did work :O Problem is, I don’t want to have it inline. Any suggestions?

Note: Do bear in mind that this question is not about how there is a bug in JS, even if the title may suggest so, it is about how I can’t get it to work since I’ve probably got the syntax wrong. Please feel free to rename it if you wish.

  • 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-04T18:13:57+00:00Added an answer on June 4, 2026 at 6:13 pm

    First of all, you should access the body element through document.body. However, onload is defined on window, so you actually need:

    window.onload = init();
    

    However, that would execute the init() method and store the return value in window.onload. Obviously, that’s not what you want: you want the init function to act as onload handler. Therefore, you should have written:

    window.onload = init;
    

    Although I’d recommend using addEventListener for that as well:

    window.addEventListener("load", init);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new at this. Tried to make sense of the manual page for
As shown on this page I have tried all I can to make the
I downloaded everything from this page http://curl.haxx.se/download.html . Build libcurl with mingw32-make mingw32-ssl-zlib ,
I tried to make the button go down with margin, padding, top to be
I want to make download page after button click. This page should show some
I tried to make use of $_SERVER , but it didn't work for me.
How can I make this input transparent? <input type=text class=foo> I've tried this but
I tried to make an accordion effect with JavaScript based off this video altering
i've tried using this code and this to make a random quote generator, but
Have been struggling all day trying to make this simple example work using socket.io.

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.