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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:09:10+00:00 2026-05-28T06:09:10+00:00

I was trying to bind an on click event to a table tr in

  • 0

I was trying to bind an on click event to a table tr in JQuery. The first example below worked as expected however the second one did not. The populate_form function fired without waiting for the event.

I was wondering if someone could explain to me why?

Thanks in advance..

Example One

.click(function() { populate_form(row.name, row.age, row.dob); });

Example Two

.click(populate_form(row.name, row.age, row.dob));
  • 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-28T06:09:11+00:00Added an answer on May 28, 2026 at 6:09 am

    The quick answer, which will probably already be answered and accepted by the time that I post
    this answer, is that your second version invokes populate_form and then passes the result of the
    function
    to click. Which, in this case, isn’t what you want. What .click() is really expecting
    is a reference to a function to execute, when something triggers that event.

    That’s the quick answer; which may or may not make sense immediately. So, let’s look a
    contrived simple example. Consider the following code:

    var Foo = function() {
        var noop = function() { };
        this.callback = noop;
    }
    
    Foo.prototype.setCallback = function(callback) {
        this.callback = callback;
    }
    
    Foo.prototype.go = function() {
        this.callback.apply(this);
    }
    

    This is a gross over-simplification of what jQuery is doing, but it gets the point across.
    We have a little class, and its got two methods: setCallback and go. Now, the idea is
    that when we call go() we want to execute whatever function we have provided. Also note that
    default callback is set to a function that is empty (or noop).

    So if we do this:

    var bar = new Foo();
    bar.go()
    

    Then nothing will happen, but we wont get any errors. That’s because that empty noop function gets invoked.

    Now we have to talk about the two ways to define a function in JavaScript: A function expression and a function declaration.

    A function declration looks like this:

    function sayHelloDeclaration() {
        alert("Hello from a declaration!");
    }
    

    And if we wanted to invoke that function, we would write:

    sayHelloDeclaration();
    

    And we would see the alert. However, if we just wanted to reference the function itself (without invoking it), then we would just use its name:

    sayHelloDeclaration;
    

    Notice the lack of parentheses; that makes this just a reference to the function. It was never told to do its work, so the alert never appears. Now, let’s look back at our Foo class, and more specifically at the setCallback function:

    Foo.prototype.setCallback = function(callback) {
        this.callback = callback;
    }
    

    Notice that this method accepts one argument, and assigns it to an property on this. Basically, we want to save a reference to a function
    for later use. And lets look at the go method as well:

    Foo.prototype.go = function() {
        this.callback.apply(this);
    }
    

    This takes whatever is stored in this.callback and tries to invoke it using, .apply(), we could have also used .call() (subtle difference between those two). Or would have just said this.callback().

    So lets, look at how it would be used. If I were to write…

    function sayHelloDeclaration() {
        alert("Hello from a declaration!");
    }
    
    var bar = new Foo();
    bar.setCallback(sayHelloDeclaration);
    

    …you would not see the popup. However, adding the last line…

    function sayHelloDeclaration() {
        alert("Hello from a declaration!");
    }
    
    var bar = new Foo();
    bar.setCallback(sayHelloDeclaration);
    bar.go();
    

    … you would see the popup. This is because go(), invoked the reference to sayHello that it had kept track of. However, you second example is doing something like this:

    function sayHello() {
        alert("Hello from a declaration!");
    }
    
    var bar = new Foo();
    bar.setCallback(sayHelloDeclaration());
    

    Notice that you get the popup, however we never called bar.go(). That is because sayHello() is invoking the method.
    The return value from that function is being passed into setCallback. If we tried to call bar.go(), we would get a
    type error because we tried to set callback to something that wasn’t a function and then tried to invoke it. This is
    a “bad thing”.


    Now, what about function expressions? A function expression is a different way to define a function. Kind of the same way you would define a string or a Number in JavaScript. It has a similar but slightly different form:

    var sayHelloExpression = function() { alert("Hello from expression"); };
    

    This essentially creates a new anonymous function and assigns it to the variable sayHelloExpression. So we can use it just like out previous example:

    var bar = new Foo();
    bar.setCallback(sayHelloExpression);
    bar.go();
    

    Or we could skip assigning it to a variable all together and just say:

    var bar = new Foo();
    bar.setCallback(function() { alert("Hello from expression"); });
    bar.go();
    

    Now there are some subtle (but important) differences between function declarations and function expressions that other people have gone into in more details about, but the key to understand here is that you can talk about a function, without invoking it. You can also keep track of a reference to a function and then invoke it later.

    In your first case you are creating a function expression that is invoking another function with some arguments. Just like our alert in our function expression, except it is a user defined function. But what you are giving setCallback is still a reference to a function that won’t be invoked until later.


    Lastly, let’s look at a reasonable use to invoke a function using when dealing with a callback like this. An example would be when you have a function that returns another function. Consider this function:

    function createGreeter(name) {
        return function() {
            alert("Hello there " + name + "!!")
        }
    }
    

    This function takes one argument, and then builds and returns another function.

    var sayHiBob = createGreeter("Bob");
    sayHiBob();  // alerts "Hello there Bob!!"
    

    This function is like a little factory for us to create other functions with. In this case, it makes total sense for use to invoke this function when using it with our setCallback method, because we want to set the callback to the result of the function that we are invoking. Something like this.

    var bar = new Foo();
    bar.setCallback(createGreeter("Joe"));
    bar.go(); // alerts "Hello there Joe!!"
    

    There is plenty more to learn, but that’s my long answer to this question.

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

Sidebar

Related Questions

I am trying to bind a simple click event to an the selected option
I am trying to bind a click event to a button within a usercontrol
I'm trying to bind a click event handler to some elements that are being
With this code, I'm trying to dynamically bind a click event to an element
I'm trying to bind some list item links in jquery mobile to a click
I am trying to bind an event to a method of a particular instance
I am trying to use knockout to bind a click to a row in
I'm trying to bind three events to one selector using the live() method, but
I am working on trying to better understand the jQuery.each() method. Here's an example
I have a table as shown below. When I click a table cell(only cells

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.