Ok, I moved the main js into a function and it worked: function calculate() { var valueStart = 0; if ($('#q38481_q3').val() === '') { $('#q38481_q3').val('0') } var lunchVal = parseInt($('#q38481_q3').val()) * 50; $('#sponsorOptions :checked, #attendingOptions :checked').each (function() { valueStart += parseInt($(this).val()); }); $('#q38487_q1').val(valueStart + lunchVal); };
//Calculate fields to add up total $('#sponsorOptions :checkbox, #attendingOptions input').click( function() { calculate(); }); $('#q38481_q3').blur( function() { calculate(); }); On Dec 15, 10:56 am, Nic Hubbard <nnhubb...@gmail.com> wrote: > I am wondering what the best practice is for calulating form sections > into a form total. E.g., the top of the form has some radio buttons > with price values, when a value is selected the total field at the > bottom of the form is updated. There is also a set of checkboxes with > values, with a click event, clicking any of these will update the > value of the total input field. > > The problem I run into is calculating vars when they are inside > different functions. How does one go about doing this? > > Here is what I have: > > //Find if user is attending > $('#attendingOptions input').change(function() { > if ($('#q38480_q1_0:checked').length === 1) { > var attendVal = 175; > $('#q38487_q1').val('175'); > } else { > var attendVal = 0; > $('#q38487_q1').val('0'); > } > }); > > //Calculate awards lunch value > var lunchVal = parseInt($('#q38481_q3').val()) * 50; > > $('#sponsorOptions :checkbox').click( > function() { > var valueStart = 0; > $('#sponsorOptions input:checkbox:checked').each(function() { > valueStart += parseInt($(this).val()); > }); > $('#q38487_q1').val(valueStart + attendVal); > }) > > Right now the calculation does not work because attendVal is inside > another function.