As a method argument in jquery without writing the definition of the whole function

From what I understand, in jquery, when a method requires a function as an argument, you cannot just call a predefined function as follows:

$('#target').click(myFunction());

... because "myFunction ()" will be parsed and replaced with the value returned by the function ... which is no longer a function. You must put the definition of the entire function in an anonymous function:

$('#target').click(function() {
   alert('Handler for .click() called.');
});

... so is there a way to just call a function where it is needed as an argument?

+3
source share
2 answers

. jQuery, JavaScript. , , . :

$('#target').click(myFunction);

, .

+4

, . :

click(myFunction());

click(myFunction);

Live jsfiddle.

+2

Source: https://habr.com/ru/post/1796517/


All Articles