Take a look at the API page. You will see that there is a way to call addClass , which passes the function as the first argument. The function receives the element and returns the names of the classes to be added:
A function that returns one or more class names, separated by spaces, to be added. Gets the index position of the element in the set and the value of the old class as arguments. Inside the function, this refers to the current element in the set.
So you can do this:
$('.someElements').addClass(function(i, currentClass) { return 'newClass' + i; });
In the first selected element, the class newClass0 will be added, the second newClass1 , etc.
With the code you sent:
if (jQuery.isFunction(value)) { return this.each(function (j) { jQuery(this).addClass(value.call(this, j, this.className)); }); }
It says:
- If the first argument was a function
- Scrolling through all selected items
- For each of them, call a function with the element as context (
this value), position in the loop as the first argument and the className property as the second argument). - Then call the
addClass method with the result of step # 3.
source share