Check out the LimeSurvey source code on GitHub!

Numeric answer calculations on the same page, then compare to a 'cut-off' number

More
5 years 7 months ago - 5 years 7 months ago #63739 by doodsonj_duplicate
Hi Team,

Is it possible to add up all the values in several numeric text fields, and then determine whether this is equal to a certain number (i.e. do the values that the user has inputted in the numeric text answer fields for Q1, Q2, Q3, Q4 add up to 10?).

I've seen the workaround for reusing values from previous questions, though this relies on the values being on a previous page (I have already implemented this solution). However, I'd like to do all the calculation on the same page as the answers rather than on a separate, later page.

If anybody could send me in the right direction or tell me if it is possible then that'd be great.

Thanks J.
Last Edit: 5 years 7 months ago by doodsonj_duplicate. Reason: spelling

Please Log in to join the conversation.

More
5 years 7 months ago #63748 by tpartner
When do you want to make the calculation? On submit, on text field change or some other trigger?

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 7 months ago #63751 by doodsonj_duplicate
Hi tpartner,

Thanks for getting back to me :)

Preferably the event would occur on a textfield.onChange event, but it would be useful to know the syntax for the onSubmit (or if this is documented elsewhere).

J.

Please Log in to join the conversation.

More
5 years 7 months ago - 5 years 7 months ago #63754 by tpartner
To calculate the total of all numeric and multi-numeric inputs when any are changed:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
		// Listener on all numeric and multi-numeric inputs
		$('.numeric-multi input.text, .numeric input.text').change(function(){
			var inputTotal = 0;
			$('.numeric-multi input.text, .numeric input.text').each(function(){
				inputTotal = inputTotal + Number($(this).val());
			});
			alert (inputTotal);
		});
 
	});
 
</script>

To calculate the total of all numeric and multi-numeric inputs when next/submit is clicked:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
		// Interrupt the Next/Submit function
		$('form#limesurvey').submit(function(){	
 
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			var inputTotal = 0;
			$('.numeric-multi input.text, .numeric input.text').each(function(){
				inputTotal = inputTotal + Number($(this).val());
			});
			alert (inputTotal);
			return true;
 
		});
 
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 7 months ago by tpartner.
The following user(s) said Thank You: doodsonj_duplicate

Please Log in to join the conversation.

More
5 years 7 months ago #63756 by doodsonj_duplicate
Excellent, I have plugged this into my code and everything is now being done as required. As ever and again... thanks very much Tony :)

J.

Please Log in to join the conversation.

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