Set attributes - attr()
The jQuery attr() method is also used to set/change attribute values.
The following example demonstrates how to change (set) the value of the href attribute in a link:
Example
Example
Callback function of attr()
jQuery method attr() also provides callback function. The callback function takes two parameters: the index of the current element in the selected element list, and the original (old) value. Then return the string you wish to use as the function's new value.
The following example demonstrates the attr() method with a callback function:
Example