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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:27:57+00:00 2026-06-18T00:27:57+00:00

Possible Duplicate: Why does click event handler fire immediately upon page load? There is

  • 0

Possible Duplicate:
Why does click event handler fire immediately upon page load?

There is a gap in my understanding of Javascript’s function so I have trouble understanding why my event handlers get fired automatically if I define it without an anonymous wrapper.

HTML

<a href="#" id="change-html">Change HTML</a>

Javascript #1

var btn = $('#change-html');
btn.click(bindClick(btn)); // bindClick gets executed right away and only once

function bindClick(source){
    console.log('get here');
}

Javascript #2

var btn = $('#change-html');
btn.click(function(){
    bindClick(btn); // bindClick is only executed on the anchor's click event 
});

function bindClick(source){
    console.log('get 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-18T00:27:58+00:00Added an answer on June 18, 2026 at 12:27 am

    Actually here

    btn.click(bindClick(btn)); 
    

    you are just binding the return value of the function to the click event, not the function itself.
    Since in javascript you can return a function this would work

    var btn = $('#change-html');
    btn.click(bindClick(btn));
    
    function bindClick(source){
        return function() {
            console.log('get here');
        }    
    }
    

    http://jsfiddle.net/VZ4Gq/

    EDIT – is the second function a closure?Yes it might be.Let’s lok at this example

    var btn = $('#change-html');
    btn.click(bindClick(btn));
    // global scope
    var inside = "i'm outside a closure";
    console.log(inside);
    function bindClick(source){
        // local scope 
        var inside = "i'm inside a closure";
        return function() {
            console.log(inside);
        }   
    }
    

    http://jsfiddle.net/VZ4Gq/1/

    when you try this you get logged “i’m outside a closure” first and then when you click on the button you get “i’m inside a closure”. this is because you actually created a closure and the ffunction, when it’s executo, it’s executed in it’s original scope, which is inside bindClick()

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

Sidebar

Related Questions

Possible Duplicate: In Javascript/jQuery what does (e) mean? jQuery's .click - pass parameters to
Possible Duplicate: In jQuery, why does programmatically triggering ‘click()’ on a checkbox not immediately
Possible Duplicate: jQuery: more than one handler for same event I am using a
Possible Duplicate: jquery - disable click How can I prevent a click event of
Possible Duplicate: jQuery - Click event doesn’t work on dynamically generated elements I just
Possible Duplicate: Iframe Function Calling From Iframe to parent page javascript function I have
Possible Duplicate: Bind an event handler on a element who’s inserted by the jQuery
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} Why does
Possible Duplicate: Does there exist a static_warning? Is there a way to implement non
Possible Duplicate: Does a exception with just a raise have any use? Is there

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.