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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:07:31+00:00 2026-06-17T06:07:31+00:00

I have a jquery dialog with a button on it (my button, not one

  • 0

I have a jquery dialog with a button on it (my button, not one of the dialog buttons).

After the dialog is set up I have the following bit of code:

$('#bar').click(foo('baz'));

When I load the page and make the dialog appear, foo is run(without pressing #bar).

If the code is:

$('#bar').click(foo);

or

$('#bar').click(function(){foo('baz')});

foo doesn’t run.

Why is that?
Pointers to where I should RTFM, gratefully received.

Just to clarify: Why does foo run in the first instance without the button being pressed (ie upon dialog initialisation) and foo not run in the latter 2 examples.

Oh, I think I just worked it out.

In 1. the result of foo is being passed to click.
In 2. I’m still not sure.
In 3. The anon function is being passed to click.

  • 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-17T06:07:32+00:00Added an answer on June 17, 2026 at 6:07 am

    This doesn’t wait for the click to call foo :

    $('#bar').click(foo('baz'));
    

    (foo('baz') is executed when this line is executed, not when the user click #bar)

    This doesn’t pass the argument to foo :

    $('#bar').click(foo);
    

    This is probably what you need (unless foo builds and returns a function, which I guess it doesn’t) :

    $('#bar').click(function(){foo('baz')});
    

    The fact it doesn’t work may be that your element with id bar isn’t found or you don’t click it.


    Detailed explanation :

    When you execute

    foo(bar); 
    

    you execute foo with argument bar.

    Wen you execute

    a = foo;
    

    you give to the variable a the value foo, which may be a function or not.

    When you call

    var a = function() {foo(bar)};
    

    you create a new anonymous function, which executes foo(bar) when called, and you gives this new function as value of the variable a.

    When you execute

    $('#bar').click(function(){foo('baz')});
    

    you pass a new function, calling foo('baz') when called, as argument to $('#bar').click(. JQuery calls this function when you click the element with id bar.

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

Sidebar

Related Questions

i have one jquery dialog which can show after click radiobutton. last time it
I have problem with Jquery UI dialog,when i click the button the first time,
I have the following code that simply uses a jquery ui modal dialog to
I have a jquery ui dialog which displays a set of buttons in initial
I have JQuery UI Dialog opening on pressing CTRL + Q using the following
I have a jQuery dialog that pops up when a button is pressed. There
I have a jQuery Dialog set to autoOpen:true . Thus it pops up on
I have a button which on clicking opens jquery dialog. Inside that jquery dialog
I have this code for a JQuery a dialog for a div containing a
In a jQuery Mobile project, have a dialog box (not a popup) with some

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.