Check out the LimeSurvey source code on GitHub!

AutoSum

More
5 years 8 months ago #61957 by RinaldoT
Hello everyone,

newbe problem: P
I have a question with multiple numeric entry type:
Men
Women
Total

How do i automatically calculate the total?

Thanks in advance

Please Log in to join the conversation.

More
5 years 8 months ago #61975 by tpartner
1) Set up your survey to use JavaScript .

2) Add the following script to the source of the multi-numeric. Replace "QQ" (line 5) with the multi-numeric question ID .

The script looks for a change in the multi-numeric inputs and if detected, adds up all but the last input and populates the last input with the total.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		totalInputs(QQ);
 
		function totalInputs(qID){	
 
			// Add a class to all inputs except the last
			$('#question'+qID+' input.text').addClass('subValue');			
			$('#question'+qID+' input.text:last').removeClass('subValue');
 
			// A listener on the numeric inputs
			$('#question'+qID+' input.text').change(function() {
 
				var total = '';
 
				// Add em up
				$('.subValue').each(function(i) {
					total = +Number(total)+Number($(this).val());
				});
 
				// Load the last input
				$('#question'+qID+' input.text:last').val(total);
			});
 
  		}
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #62001 by RinaldoT
Perfect, thank you for your reply :cheer:

One last question and do not bother you more:
if you wanted the last input (Total) was of a different color and had not changed ?
Last Edit: 5 years 8 months ago by RinaldoT.

Please Log in to join the conversation.

More
5 years 8 months ago #62022 by tpartner

...if you wanted the last input (Total) was of a different color...

Change this line:
$('#question'+qID+' input.text:last').removeClass('subValue');
To this:
$('#question'+qID+' input.text:last').removeClass('subValue').addClass('lastInput');
And then add someting like this to template.css:
.lastInput {
	color: #FF0000;
	font-weight: bold;
}

...and had not changed...

You can't disable the input or the value won't be recorded in the data, however, the script recalculates when the last input is changed, effectively disabling it.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 8 months ago #62024 by RinaldoT
Thank you were very kind

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form