Jquery / javascript: what does "i" in do? (Function (i) {"do / mean?" How is this known?

In the example below, jQuery code changes the src of the images on the page, starting at "test", and ending with ".jpg", but what tells it that i? if I'm right, iit can also be numbereither e, or z, or n, or any other word or letter that I want, therefore, placing it there ihe says that it iis the current object? or object name? or is it just a variable, and if it's a variable, what tells her that the variable matters? what i? is that number? and if I changed the value ito h, would that be the same?

$("img").each(function(i){ 
  this.src = "test" + i + ".jpg"; 
});
HTML:
<img/><img/>
Result:
<img src="test0.jpg"/><img src="test1.jpg"/>

Does anyone know any good tutorials that would specifically deal with this?

+3
source share
3 answers

iis the index of the element in the set of elements found $("img").

Callback parameters can be found in the documentation.each() :

.each (function (index, element))
function (index, Element) - a function that is executed for each matched element.

As for changing it, yes, you can call the parameter you want (as long as it is not a keyword) and use this name in this function.

+2
source

jQuery.each docs , . : " , , , 0." i.

+2

Changing the variable name from I to any other name will not affect the type of the variable. In this case, it is an integer.

0
source

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


All Articles