How does a function work inside a function?

HTML

<p>This is a <b>bold</b> paragraph.</p> <button>Add</button> 

JQuery

 $("button").click(function(){ $("p").text(function(i,origText){ return "Old text: " + origText + " New text: Hello world! (index: " + i + ")"; }); }); 

I would like to know that origText is not called an external function, but returns a value. How?

demo

+4
source share
4 answers

As the docs say

function (index, text)

A function that returns text content for installation. Gets the index position of the element in the set and the old text value as arguments.

And How does jQuerys.text () work inside?

+2
source

The actual text method takes a function as a parameter. The function passed to text can contain two parameters, the first of which gets the index, the second - the source text.

+2
source

The text function performs some preparations on the input nodes, and then performs a callback provided by the user (your function), and passes these two arguments (which were prepared on "some preparations") to your callback. If you need the exact code, you can read the jQuery source.

0
source

In simple:

 text(function(whatever){ return whatever; // returns the text what is the text it has. }); 

In your example p there is text: This is a bold paragraph.

return everything that your text returns: This is a bold paragraph.

0
source

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


All Articles