Welcome, Guest
Username: Password: Remember me

TOPIC: AutoSum

AutoSum 3 years 2 months ago #61957

  • RinaldoT
  • RinaldoT's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
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
The administrator has disabled public write access.

Re: AutoSum 3 years 2 months ago #61975

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4117
  • Thank you received: 760
  • Karma: 346
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: AutoSum 3 years 2 months ago #62001

  • RinaldoT
  • RinaldoT's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
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: 3 years 2 months ago by RinaldoT.
The administrator has disabled public write access.

Re: AutoSum 3 years 2 months ago #62022

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4117
  • Thank you received: 760
  • Karma: 346
...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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: AutoSum 3 years 2 months ago #62024

  • RinaldoT
  • RinaldoT's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 14
  • Karma: 0
Thank you were very kind
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.121 seconds
Donation Image