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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:33:57+00:00 2026-06-11T22:33:57+00:00

I have this code blocks from W3Schools sample of JQuery. Try it on W3Schools

  • 0

I have this code blocks from W3Schools sample of JQuery. Try it on W3Schools . I have only added one callback on the hide method call.

First Case

 <!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script> function sayHello(){ alert('hello sit'); }

$(document).ready(function(){   $("button").click(function(){
    $("p").hide(100,sayHello);   }); });

</script> </head> <body> <button>Hide</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body> </html>

The second case is

 <!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script> function sayHello(){ alert('hello sit'); }

$(document).ready(function(){   $("button").click(function(){
    $("p").hide(100,sayHello());   }); });

</script> </head> <body> <button>Hide</button> <p>This is a paragraph with little content.</p> <p>This is another small paragraph.</p> </body> </html>

The only difference in both is passing the callback function with or without braces.

In first case, on click of hide button I am getting hello alert twice.
In second case I am getting hello alert only once.
I am expecting the alert to come only once in both cases as it doesn’t matter for a zero args function to be called with or without braces.

I would like to understand why it is calling callback function twice when function name is passed without braces.

  • 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-11T22:33:58+00:00Added an answer on June 11, 2026 at 10:33 pm

    In the second case, you’re passing the result of evaluating sayHello(), whereas in the first case, you’re passing the actual function sayHello.

    So because sayHello shows an alert, it will show the alert as soon as it’s executed. In the first case, it’s executed when hide is complete (as a callback), whereas in the second case it’s executed when you set up the call to hide() (probably before the hide actually occurs).

    Not sure why it’s executing twice, but (see below) the version without parenthesis (version 1) is probably the version you’re looking for if you want it to execute once hide() is complete.

    UPDATE:

    The reason it’s showing the alert twice is that you’re calling hide() twice. You have two different <p> elements, and .hide() is being called for each of them in turn (and the callback is being executed for each in turn likewise). I would guess if you add a third <p> tag it will alert a third time.

    From the JQuery Docs:

    If multiple elements are animated, it is important to note that the callback is executed once per matched element, not once for the animation as a whole.

    You probably want to encompass your elements in a parent element (like a <div>) and hide that instead of each individual <p> element.

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

Sidebar

Related Questions

In my code, I have this loop, which brings up various 2-hour time blocks.
I have ported some code from Mingw which i wrote using code::blocks, to visual
I have this code: class StoryViewClass(ListView): ... some listview methods here for one set
I have to find the code blocks from the given code using a regex
I have written some code using jQuery to use Ajax to get data from
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,
I have this block of code where when I hover (click for purposes of
i have seen this block of code in this Stackoverflow as a answer of
I have seen this symbol/operator in a block of code: a+=1; But I cannot

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.