Formidable Forms Pro and Javascript calculations

Hi,

I have a website using Formidable Forms to create a scored questionnaire, which has some optional questions (user can select N/A). The average score is calculated if they select N/A, and so the denominator in the average is required. I first achieved this by using some javascript to count the number of radio buttons being checked.

E.g. for the first 5 questions, this works fine using this code here:

<script type="text/javascript">
jQuery(document).ready(function($){
$('input[name="item_meta[343]"], input[name="item_meta[344]"], input[name="item_meta[345]"], input[name="item_meta[346]"], input[name="item_meta[347]"]').change(function(){
var val = new Array(343, 344, 345, 346, 347);
var total = 0;
for ( i = 0; i < val.length; i++ ) {
  val[i] = $("input[name='item_meta[" + val[i] + "]']:checked").val();
   if ( val[i] == undefined || val[i] == "N/A" ) {
       val[i] = 0;
   } else {
      val[i] = 1;
   }
   total += val[i];
}
{
$("#field_spadi_pq_tally").val(total);
$("#field_spadi_pq_tally").change();
}
});
});
</script>

The code currently uses the Field ID's (numbers) to reference the radio buttons. I would like to use the Field Key's instead. These can be seen by viewing the page source on my form/page here:. Line 400. Field ID in formidable = (name="item_meta[343]"), and field key = (id="field_spadi_pq1-0"). The benefit of this is that the field key's never change, but the field id's do automatically change if forms are deleted/added etc.

I have been trying to write some code with the help of the formidable support team, however they are unable to assist further due to it extending beyond their scope. They gave me the reference for the field keys, but I can still not get it to work.

Was wondering if you could help with what I have so far...

<script type="text/javascript">
jQuery(document).ready(function($){
$('input[id^="field_spadi_pq_q1-"], input[id^="field_spadi_pq_q2-"]').change(function(){
var val = new Array('spadi_pq_q1-', 'spadi_pq_q21');
var total = 0;
for ( i = 0; i < val.length; i++ ) {
  val[i] = $("input[id^=' + val[i] + ']:checked").val();
   if ( val[i] == undefined || val[i] == "N/A" ) {
       val[i] = 0;
   } else {
      val[i] = 1;
   }
   total += val[i];
}
{
$("#field_spadi_pq_tally").val(total);
$("#field_spadi_pq_tally").change();
}
});
});
</script>