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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:03:36+00:00 2026-05-24T18:03:36+00:00

I playing around with a function that I want to bind to all the

  • 0

I playing around with a function that I want to bind to all the links. At the present the function fires when the page loads, instead of when I click on the link.

Here’s my code. (I can post the function showDiv(), if you need to see it.) Can you tell if I’m doing something wrong or stupid here?

$(document).ready(function(){

    $('a.test').bind("click", showDiv());

});

Thanks!

  • 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-24T18:03:37+00:00Added an answer on May 24, 2026 at 6:03 pm

    You want to pass a reference to a function as a callback, and not the result of function execution:

    showDiv() returns some value; if no return statement was used, undefined is returned.

    showDiv is a reference to the function that should be executed.

    This should work:

    $(document).ready(function() {
      $('a.test').on("click", showDiv); // jQuery 1.7 and higher
      $('a.test').bind("click", showDiv); // jQuery 1.6 and lower
    });
    

    Alternatively, you could use an anonymous function to perform a more advanced function:

    // jQuery 1.7 and higher
    el.on('click', function() {
      foo.showDiv(a, b, c);
      // more code...
    });
    
    // jQuery 1.6 and lower
    el.bind('click', function() {
      foo.showDiv(a, b, c);
      // more code...
    });
    

    In some circumstances you may want to use the value returned by a function as a callback:

    function function foo(which) {
      function bar() {
        console.log('so very true');
      }
    
      function baz() {
        console.log('no way!');
      }
    
      return which ? bar : baz;
    }
    
    el.click(foo(fizz));
    

    In this example, foo is evaluated using fizz and returns a function that will be assigned as the callback for the click event.

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

Sidebar

Related Questions

i'm playing around with building a sql function that will extract numbers from a
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I am playing around with lisp's format function, but I have hit a snag
I was playing around with recursion and did this simple function. I was assuming
There is an eval() function in Python I stumbled upon while playing around. I
I'm playing around with beginner Haskell, and I wanted to write an average function.
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
I've been playing around trying to create a transparent window that will overlay another
I am playing around to develop a sampling function to do randomization to make
I am playing around with the Google Maps API V3. I want to create

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.