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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:31:48+00:00 2026-06-01T06:31:48+00:00

I am trying to understand the Javascript event handling. I like some code to

  • 0

I am trying to understand the Javascript event handling. I like some code to be called when I press a button and added an eventlistener – my code:

<html>
 <head>
    <title>
        PDSA #4.4: Event Registering
    </title>
    <script>
        function changeTheButton(btn){
            btn.innerHTML = "Please, don't press again!";
            alert("Changed the button");
        }

        function init(){
            btn = document.getElementById("button1");
            btn.addEventListener("click", changeTheButton(btn), false);
        }
    </script>
 </head>
 <body onload="init()">
    <form>
        <button id="button1">Don't press this button</button>
    </form>
 </body>
</html>

When I open this site in Chrome or Firefox the text on the button is already “Please, don’t press again!” and the alert is opened before I do anything.

Why does this happen? I expected this to happen, when I press the button and not on load.

Thanks in advance for any help
André

  • 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-01T06:31:50+00:00Added an answer on June 1, 2026 at 6:31 am

    Watch this line:

    btn.addEventListener("click", changeTheButton(btn), false);
    

    You’re actually calling changeTheButton right there, resulting in the alert. You probably want to register it as an event handler instead, thus deferring the alert until the button is pressed:

    btn.addEventListener("click", function() { changeTheButton(btn) }, false);
    

    You can watch the result at jsFiddle.

    One additional problem you might experience is that the button click causes a page reload, because the default event handler is still called. You can prevent that by calling .preventDefault() on the event instance:

    function changeTheButton(ev, btn) {
        alert("Changed the button");
        btn.innerHTML = "Please, don't press again!";
        ev.preventDefault();
    }
    
    function init(){
        btn = document.getElementById("button1");
        btn.addEventListener("click", function(ev) { 
            changeTheButton(ev, btn) 
        }, false);
    }
    

    Demo.

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

Sidebar

Related Questions

I am trying to understand how to develop stand-alone Javascript code. I want to
I am trying to understand how to chain JavaScript events together like jQuery does.
I'm trying to understand javascript scopes, and I want to know if in the
I'm trying to understand how to use firebug to debug my Javascript. So I
I am trying to understand how a single and multidimensional javascript array would appear
I'm trying to understand how to build a JSON object in JavaScript. This JSON
I am trying to pass a value from php to javascript. I understand one
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
In trying to call a method on the CodeMirror javascript code editor. I'm new

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.