JavaScript (Why) does jQuery .click() require a callback function? what does jquery click return,how does jquery click work,

I have the following jQuery code:

function next() {
    //some code here
}

function previous() {
    //some code here
}

$("#next").click(function(){
    next();
});

$("#previous").click(function(){
    previous();
});

This works, but this doesn't:

$("#next").click(next());

$("#previous").click(previous());

Why is this happening? Is there a problem in my code, or is this just a thing with jQuery? Note: #next and #previous refer to two buttons in my html file.

Answer:1

This works (if the functions next and previous are defined):

$("#next").click(next);

$("#previous").click(previous);

In this case the next and previous are also callback functions, the difference between the two is,

when you call this line

$("#next").click(next()); the function is executed immediately, and you are passing the result of the next function to the eventHandler of jQuery.

and in this case

$("#next").click(next); you are passing the function next to the EventHandler of jQuery.

Btw.: in the jQuery API Documentation (https://api.jquery.com/click/) it shows all parameters for the click function and the required types it states: "...handler Type: Function( Event eventObject ) A function to execute each time the event is triggered. ..."

Answer:2

The callback should be a reference to the function.

Why $("#next").click(next()); doesn't work?

func() is a function call and not a reference, which is why it is called immediately.


This,

$("#next").click(function(){
    next();
});

is a preferable way in case you need to pass arguments.

Else,

$("#next").click(next) //notice just the signature without ()
Answer:3

try like this you will get your answer,

function next() {
    //some code here
}

function previous() {
    //some code here
}

$("#next").click(next);

$("#previous").click(previous);

working demo jsfiddle Example

Answer:4

What is going on there is a little bit obscured by the syntax of anonymous functions function() { ... }. What you are doing by that is passing a function, without calling it. And I want to explain how this works:

If you have a simple function

function next() { return 5 };

It will simply return the value 5, if you call it from somewhere:

a = next(); // value of a will be 5

But what you can do too, is to pass the whole function to a. This is possible, because functions in JavaScript are actually objects:

a = next;
b = a();    // value of b will be 5

If you look at the syntax, it shows you, that putting parentheses () at the end of a function invokes it, and returns the return value. While the naked string, without parentheses hands you the function itself.

So what is a callback now, and what does click() like to get as a parameter? A callback function is a function, that gets called later; we actually hand it over, to get called later. click() would like to get such a function as parameter, and it should be clear now, that we have to pass the function without parentheses, to enable click() to call it later, instead of just passing a 5 to it.

$("#next").click(next);

So how does then the initial syntax with the anonymous function work?

function() { next(); }

actually wraps your next() into another function, which is anonymous – because it does not have a name – but is working in the same way as a named function. You can even set a variable by it:

a = function() { next(); }  // a will be the anonymous function that calls next()

But calling that function a() will return nothing, because the anonymous function does not return a value (To be exactly: every function call in JavaScript is returning at least undefined, but that's a technical detail). It can even be called immediately by putting parenthesis at the end of it:

a = function() { return next(); }()   // value of a will be 5

Adding the return there will make sure, the return value of next() will be passed through the anonymous function.

This should make clear why

$("#next").click(function(){ next(); });

is working, and why

$("#next").click(next());

is not, but

$("#next").click(next);

will be a good solution.

Answer:5

I'm trying to make a function which is similar to React's createClass function. It should take a POJO and turn it into constructor function which can be invoked with additional arguments. Here's the ...

I'm trying to make a function which is similar to React's createClass function. It should take a POJO and turn it into constructor function which can be invoked with additional arguments. Here's the ...

  1. strange bird behavior
  2. strange bird behavior 2019
  3. strange bird behavior 2018
  4. strange bird behavior window
  5. strange wild bird behavior
  6. love bird strange behavior

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

  1. react dynamically insert component

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

  1. reactjs state undefined in function
  2. reactjs undefined is not a function
  3. react js function undefined
  4. react state undefined in function

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

  1. problems with calling on iphone
  2. problems with calling 911
  3. problems with calling ios
  4. problems calling with o2
  5. problems with wifi calling
  6. problems with wifi calling on iphone 7
  7. problems with wifi calling iphone 6
  8. problems with cold calling
  9. problems with wifi calling iphone x
  10. problems with alexa calling
  11. problems with wifi calling on iphone 8
  12. problems with video calling
  13. problems with wifi calling on ee
  14. problems with wifi calling on iphone xr
  15. problems with wifi calling iphone
  16. problems with conference calling
  17. problems calling org.antlr.v4.gui.testrig.main(args)
  18. problems calling out iphone
  19. problems calling germany
  20. problems calling mexico