Okay, so here is a test survey.
In group 1, there is a multiple-choice followed by a short-text (note that the short-text must be place directly after the multiple-choice).
"Scores" are appended to the sub-question codes of the multiple-choice.
The short-text contains the script below that:
- Hides the short-text
- Puts a listener on the multiple-choice checkboxes
- When a checkbox is changed, the scores for all checked options are totaled and loaded into the short-text.
Group 2 simply contains a text-display question that presents the score.
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify the questions
var qID = {QID};
var thisQuestion = $('#question'+qID);
var prevMultiChoice = $(thisQuestion).prevAll('.multiple-opt:eq(0)');
// Hide this question
$('#question'+qID).hide();
// Find the initial score
var totalScore = 0;
$('input.checkbox:checked', prevMultiChoice).each(function(i) {
var thisScore = $(this).attr('id').slice(-1);
totalScore = totalScore + Number(thisScore);
});
// Store the score
$('input[type="text"]', thisQuestion).val(totalScore);
// Listener on the checkboxes
$('input.checkbox', prevMultiChoice).change(function(event) {
// Find the score
totalScore = 0;
$('input.checkbox:checked', prevMultiChoice).each(function(i) {
var thisScore = $(this).attr('id').slice(-1);
totalScore = totalScore + Number(thisScore);
});
// Store the score
$('input[type="text"]', thisQuestion).val(totalScore);
});
});
</script>
.