I have made a simple scorecard form which uses jQuery to automatically calculate the score of submissions depending on field values. Some fields are critical which means if they are a fail they automatically fail. You can see a jsFiddle example to get a better understanding. I have tried to make the code as efficient as possible but would welcome peoples opinions on what improvements can be made.//disable individual radios by#$(#edit-submitted-calc-verification-completed-idv-correctly-1, #edit-submitted-calc-summary--action-2-notes-1).attr('disabled',true);$(#webform-component-calc select, #webform-component-calc :radio).change(function(){   var $score = 0;    var $fails = 0;   var $critFails = 0;    $('select option:selected, :radio:checked').each(function(){         var $thisVal = $(this).val();        var $split = $thisVal.split('_');        var $string = $split[0];        var $points = parseInt($split[1],10);      $(this).parents('tr').find('.webform-grid-option').removeClass('pass fail');      if ($string == 'fail' || $string == 'critical') {          $(this).parents('.webform-grid-option').addClass('fail');          if($string == 'fail') {              $fails += 1;          }          if($string == 'critical') {              $critFails += 1;             }      }      else {          $(this).parents('.webform-grid-option').addClass('pass');           if($string == 'pass' || $string == 'na') {              $score += $points;             }                   }    });    var $finalScore = $score;    if($critFails > 0){         var $finalScore = 0;       }    var $summary = 'Final Call score: '+$finalScore+'% with '+$fails+' Fails / '+$critFails+' Critical Fails';    //alert($summary);    $('#edit-submitted-calc-number-of-fails').val($fails);     $('#edit-submitted-calc-call-score').val($score);     $('#edit-submitted-calc-critical-failures-number-of-critial-fails').val($critFails);     $('#edit-submitted-calc-critical-failures-final-score').val($finalScore);     $('#edit-submitted-calc-summary').val($summary); });
jQuery score card form to determine pass or fail
javascript;jquery
Personally I wouldn't change anything about the calculation itself. It all quite reasonable the way it is. There are just two things could be optimized concerning jQuery:You should be using .closet() instead of .parents() since you only need the first (closest) matching parent and not all ancestors.Also you can optimize assigning the event handler to the radio buttons by using .delegate() (or .on with jQUery 1.7) instead.There are also a few things which personally I'd do differently:In the HTML I'd use th elements for the question cells and td just for the options, allowing you to get rid of the classes webform-grid-question and webform-grid-option.I'm not a big fan of using $ in variable names, especially if they don't point to a jQuery object.I don't like using input elements for output. I'd use a normal element such as span or div (or if HTML5 is a option the new output element).