The script below is throwing an error (customfields is not defined). Do I need to pass the element IDs differently?
I'm trying to seed the array with the form fields that I'm looking to calculate. It should iterate through each of the form fields in the array and increment the sum variable with the value of the form element.
jQuery(document).ready(function(){
jQuery("#customfield_21070").attr('style','width:60px');
jQuery("#customfield_21070").attr('disabled','disabled');
var customfields = [
'#customfield_11070',
'#customfield_11071',
'#customfield_20071',
'#customfield_20072',
'#customfield_20073',
'#customfield_20074'
];
jQuery(customfields).each(function() {
jQuery(this).attr('style','width:60px');
jQuery(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
jQuery(customfields).each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0 && this.id !== "customfield_21070") {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
jQuery("#customfield_21070").val(sum.toFixed(2));
}