It should just be looping through each field on the table to subscribe to the event, and when the blur happens, it updates every field of that ID (so every field in the table is updated every time that any field in that column of the table is updated). You can confirm that the loop isn't happening too much if you add something like console.log('test'); within it, then check the console and you'll see that it is only triggering at the expected time period.
If you wanted to be able to re-use the code for multiple fields, then we can wrap most of that code in a function block, and then just call the function with the desired field ID numbers, like this:
//Call the function for all of the needed fields (by fieldID Number).
AddCapitalizeOnBlur(2);
AddCapitalizeOnBlur(3);
AddCapitalizeOnBlur(4);
AddCapitalizeOnBlur(5);
//Function to add the toCapitalize functionality to any field.
//You just need to pass in the field ID Number.
function AddCapitalizeOnBlur(fieldIDNumber)
{
//Retrieve all fields with that indicated ID.
var myFields = LFForm.findFieldsByFieldId(fieldIDNumber);
//Loop through all fields with the indicated ID and
//add an onFieldBlur listener.
myFields.forEach(function (arrayItem) {
LFForm.onFieldBlur(function(){
//Retrieve all values from the field(s) in the table.
var newValues = LFForm.getFieldValues(arrayItem);
//Loop through all field values if 2 or more were found,
//and call the toCapitalize function.
if(newValues.constructor === Array) {
for (var i = 0; i < newValues.length; i++) {
newValues[i] = toCapitalize(newValues[i]);
}
}
//Otherwise call the toCapitalize on the single string value.
else {
newValues = toCapitalize(newValues);
}
//Return the updated array of field values back to the table.
LFForm.setFieldValues({fieldId: fieldIDNumber}, newValues);
}, arrayItem); //End of LFForm.onFieldBlur...
}); //End of myFields.forEach...
} //End of function AddCapitalizeOnBlur(fieldIDNumber)...
//Function to capitalize the first letter of each
//word in a string.
function toCapitalize (maniptxt) {
var arr = new Array();
arr = maniptxt.split(" ");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
return arr.join(" ");
}
In that example, I'm adding the functionality to field IDs 2, 3, 4, and 5 (the first several lines of code) by calling the function 4 times and passing in each ID. You can see in this screenshot (I wrote everything entirely in lower case) that it is working for fields 2 and 3 in the table, and for fields 4 and 5 outside the table:
