Changing Attribute Values Using a Function in jQuery
You can also apply a function to each attribute. In this example, there are three inputs:
<input type="text" value="aaa">
<input type="text" value="bbb">
<input type="text" value="ccc">
Let's change the value attribute for each input - add the ordinal number of the element in the set to the current value:
$('input').attr('value', function(index, value) {
return value + ' ' + index;
});
Thus, all values of the attribute value will be replaced by those that the function returns to us. The HTML code will look like this:
<input type="text" value="aaa 0">
<input type="text" value="bbb 1">
<input type="text" value="ccc 2">
Change the value of value every second input by adding the element's ordinal number in the set to the current value.